From 2d77565e6cbdc26e91a2c1459bf90da79e70c23a Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Wed, 29 Aug 2018 12:19:33 +1000 Subject: [PATCH] Peer review 52d1388e4e7d7165695af889f8dcc861086ad012 This uses a simpler approach and leverages improvements to the validation trait See https://github.com/octobercms/library/commit/574031d3eea051dce2bcf7079285262ad4f7ac44 Refs #2489 --- modules/backend/behaviors/FormController.php | 24 -------------------- modules/backend/widgets/Form.php | 21 ++++++++++------- 2 files changed, 13 insertions(+), 32 deletions(-) diff --git a/modules/backend/behaviors/FormController.php b/modules/backend/behaviors/FormController.php index 71e9567f6..86c47b42c 100644 --- a/modules/backend/behaviors/FormController.php +++ b/modules/backend/behaviors/FormController.php @@ -233,7 +233,6 @@ class FormController extends ControllerBehavior $this->initForm($model); - $this->controller->formSetValidationNames($model); $this->controller->formBeforeSave($model); $this->controller->formBeforeCreate($model); @@ -301,7 +300,6 @@ class FormController extends ControllerBehavior $model = $this->controller->formFindModelObject($recordId); $this->initForm($model); - $this->controller->formSetValidationNames($model); $this->controller->formBeforeSave($model); $this->controller->formBeforeUpdate($model); @@ -681,28 +679,6 @@ class FormController extends ControllerBehavior // 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 diff --git a/modules/backend/widgets/Form.php b/modules/backend/widgets/Form.php index 01e96f23f..68a1768a0 100644 --- a/modules/backend/widgets/Form.php +++ b/modules/backend/widgets/Form.php @@ -783,9 +783,11 @@ class Form extends WidgetBase list($fieldName, $fieldContext) = $this->getFieldName($name); $field = new FormField($fieldName, $label); + if ($fieldContext) { $field->context = $fieldContext; } + $field->arrayName = $this->arrayName; $field->idPrefix = $this->getId(); @@ -793,20 +795,17 @@ class Form extends WidgetBase * 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( @@ -824,7 +823,6 @@ class Form extends WidgetBase } $field->displayAs($fieldType, $config); - } /* @@ -832,12 +830,19 @@ class Form extends WidgetBase */ $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 @@ -852,10 +857,10 @@ class Form extends WidgetBase } // 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); } /*