This uses a simpler approach and leverages improvements to the validation trait
See 574031d3ee
Refs #2489
This commit is contained in:
Samuel Georges 2018-08-29 12:19:33 +10:00
parent 52d1388e4e
commit 2d77565e6c
2 changed files with 13 additions and 32 deletions

View File

@ -233,7 +233,6 @@ class FormController extends ControllerBehavior
$this->initForm($model); $this->initForm($model);
$this->controller->formSetValidationNames($model);
$this->controller->formBeforeSave($model); $this->controller->formBeforeSave($model);
$this->controller->formBeforeCreate($model); $this->controller->formBeforeCreate($model);
@ -301,7 +300,6 @@ class FormController extends ControllerBehavior
$model = $this->controller->formFindModelObject($recordId); $model = $this->controller->formFindModelObject($recordId);
$this->initForm($model); $this->initForm($model);
$this->controller->formSetValidationNames($model);
$this->controller->formBeforeSave($model); $this->controller->formBeforeSave($model);
$this->controller->formBeforeUpdate($model); $this->controller->formBeforeUpdate($model);
@ -681,28 +679,6 @@ class FormController extends ControllerBehavior
// Overrides // 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. * Called before the creation or updating form is saved.
* @param Model * @param Model

View File

@ -783,9 +783,11 @@ class Form extends WidgetBase
list($fieldName, $fieldContext) = $this->getFieldName($name); list($fieldName, $fieldContext) = $this->getFieldName($name);
$field = new FormField($fieldName, $label); $field = new FormField($fieldName, $label);
if ($fieldContext) { if ($fieldContext) {
$field->context = $fieldContext; $field->context = $fieldContext;
} }
$field->arrayName = $this->arrayName; $field->arrayName = $this->arrayName;
$field->idPrefix = $this->getId(); $field->idPrefix = $this->getId();
@ -793,20 +795,17 @@ class Form extends WidgetBase
* Simple field type * Simple field type
*/ */
if (is_string($config)) { if (is_string($config)) {
if ($this->isFormWidget($config) !== false) { if ($this->isFormWidget($config) !== false) {
$field->displayAs('widget', ['widget' => $config]); $field->displayAs('widget', ['widget' => $config]);
} }
else { else {
$field->displayAs($config); $field->displayAs($config);
} }
} }
/* /*
* Defined field type * Defined field type
*/ */
else { else {
$fieldType = isset($config['type']) ? $config['type'] : null; $fieldType = isset($config['type']) ? $config['type'] : null;
if (!is_string($fieldType) && !is_null($fieldType)) { if (!is_string($fieldType) && !is_null($fieldType)) {
throw new ApplicationException(Lang::get( throw new ApplicationException(Lang::get(
@ -824,7 +823,6 @@ class Form extends WidgetBase
} }
$field->displayAs($fieldType, $config); $field->displayAs($fieldType, $config);
} }
/* /*
@ -832,12 +830,19 @@ class Form extends WidgetBase
*/ */
$field->value = $this->getFieldValue($field); $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 * Check model if field is required
*/ */
if ($field->required === null && $this->model && method_exists($this->model, 'isAttributeRequired')) { if ($field->required === null && $this->model && method_exists($this->model, 'isAttributeRequired')) {
$fieldName = implode('.', HtmlHelper::nameToArray($field->fieldName));
// Check nested fields // Check nested fields
if ($this->isNested) { if ($this->isNested) {
// Get the current attribute level // Get the current attribute level
@ -852,10 +857,10 @@ class Form extends WidgetBase
} }
// Recombine names for full attribute name in rules array // 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);
} }
/* /*