Skip to content

Commit

Permalink
Peer review 52d1388
Browse files Browse the repository at this point in the history
This uses a simpler approach and leverages improvements to the validation trait
See octobercms/library@574031d
Refs octobercms#2489
  • Loading branch information
daftspunk committed Aug 29, 2018
1 parent 52d1388 commit 2d77565
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 32 deletions.
24 changes: 0 additions & 24 deletions modules/backend/behaviors/FormController.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@ public function create_onSave($context = null)

$this->initForm($model);

$this->controller->formSetValidationNames($model);
$this->controller->formBeforeSave($model);
$this->controller->formBeforeCreate($model);

Expand Down Expand Up @@ -301,7 +300,6 @@ public function update_onSave($recordId = null, $context = null)
$model = $this->controller->formFindModelObject($recordId);
$this->initForm($model);

$this->controller->formSetValidationNames($model);
$this->controller->formBeforeSave($model);
$this->controller->formBeforeUpdate($model);

Expand Down Expand Up @@ -681,28 +679,6 @@ public function formGetSessionKey()
// Overrides
//

/**
* Called before the creation or updating form is saved to override the field names for validation using their labels
*/
public function formSetValidationNames($model)
{
$attributeNames = [];
$form = $this->formGetWidget();
foreach ($form->getFields() as $field) {
$fieldName = implode('.', HtmlHelper::nameToArray($field->fieldName));
$attributeNames[$fieldName] = $field->label;
}
if (!property_exists($model, 'attributeNames')) {
$model->addDynamicProperty('attributeNames', $attributeNames);
// Clean up after validation to prevent attributeNames from being handled as a model attribute to be saved in the DB
$model->bindEvent('model.afterValidate', function () use ($model) {
unset($model->attributes['attributeNames']);
});
} else {
$model->attributeNames = array_merge($attributeNames, (array) $model->attributeNames);
}
}

/**
* Called before the creation or updating form is saved.
* @param Model
Expand Down
21 changes: 13 additions & 8 deletions modules/backend/widgets/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -783,30 +783,29 @@ protected function makeFormField($name, $config = [])
list($fieldName, $fieldContext) = $this->getFieldName($name);

$field = new FormField($fieldName, $label);

if ($fieldContext) {
$field->context = $fieldContext;
}

$field->arrayName = $this->arrayName;
$field->idPrefix = $this->getId();

/*
* Simple field type
*/
if (is_string($config)) {

if ($this->isFormWidget($config) !== false) {
$field->displayAs('widget', ['widget' => $config]);
}
else {
$field->displayAs($config);
}

}
/*
* Defined field type
*/
else {

$fieldType = isset($config['type']) ? $config['type'] : null;
if (!is_string($fieldType) && !is_null($fieldType)) {
throw new ApplicationException(Lang::get(
Expand All @@ -824,20 +823,26 @@ protected function makeFormField($name, $config = [])
}

$field->displayAs($fieldType, $config);

}

/*
* Set field value
*/
$field->value = $this->getFieldValue($field);

/*
* Apply the field name to the validation engine
*/
$attrName = implode('.', HtmlHelper::nameToArray($field->fieldName));

if ($this->model && method_exists($this->model, 'setValidationAttributeName')) {
$this->model->setValidationAttributeName($attrName, $field->label);
}

/*
* Check model if field is required
*/
if ($field->required === null && $this->model && method_exists($this->model, 'isAttributeRequired')) {
$fieldName = implode('.', HtmlHelper::nameToArray($field->fieldName));

// Check nested fields
if ($this->isNested) {
// Get the current attribute level
Expand All @@ -852,10 +857,10 @@ protected function makeFormField($name, $config = [])
}

// Recombine names for full attribute name in rules array
$fieldName = implode('.', $nameArray) . ".{$fieldName}";
$attrName = implode('.', $nameArray) . ".{$attrName}";
}

$field->required = $this->model->isAttributeRequired($fieldName);
$field->required = $this->model->isAttributeRequired($attrName);
}

/*
Expand Down

0 comments on commit 2d77565

Please sign in to comment.