mirror of
https://github.com/wintercms/winter.git
synced 2024-06-28 05:33:29 +02:00
Automatically use field labels as custom attribute names for a nicer validation message
Fixes #2489
This commit is contained in:
parent
fcec026dff
commit
52d1388e4e
@ -9,6 +9,7 @@ use Input;
|
||||
use Redirect;
|
||||
use Backend;
|
||||
use Backend\Classes\ControllerBehavior;
|
||||
use October\Rain\Html\Helper as HtmlHelper;
|
||||
use October\Rain\Router\Helper as RouterHelper;
|
||||
use ApplicationException;
|
||||
use Exception;
|
||||
@ -232,6 +233,7 @@ class FormController extends ControllerBehavior
|
||||
|
||||
$this->initForm($model);
|
||||
|
||||
$this->controller->formSetValidationNames($model);
|
||||
$this->controller->formBeforeSave($model);
|
||||
$this->controller->formBeforeCreate($model);
|
||||
|
||||
@ -299,6 +301,7 @@ class FormController extends ControllerBehavior
|
||||
$model = $this->controller->formFindModelObject($recordId);
|
||||
$this->initForm($model);
|
||||
|
||||
$this->controller->formSetValidationNames($model);
|
||||
$this->controller->formBeforeSave($model);
|
||||
$this->controller->formBeforeUpdate($model);
|
||||
|
||||
@ -678,6 +681,28 @@ 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
|
||||
|
Loading…
x
Reference in New Issue
Block a user