Where do I customize form validation messages in ZF3 if I'm not specifying a validator in my input filter?
If I use the code that's presented in the ZF3 documentation as shown below, the 'required' => true,
parameter will cause the formElementErrors()
helper to render the message "Value is required and can't be empty"
at form validation if the input is left empty. I'd like to change that message, but don't know where to change it. I am aware that if I define a validator in the input filter, I can customize the messages there for the validator that I define. But if I leave 'validators' => [],
as shown in the ZF3 example, where are the messages defined?
return [
'input_filter_specs' => [
'foobar' => [
[
'name' => 'name',
'required' => true,
'filters' => [
[
'name' => 'Zend\Filter\StringTrim',
'options' => [],
],
],
'validators' => [],
'description' => 'Hello to name',
'allow_empty' => false,
'continue_if_empty' => false,
],
],
],
];
In the Zend\InputFilter\Input
class in the prepareRequiredValidationFailureMessage
method the NotEmpty
validator is automatically attached to the validator chain of an element, if the element is required and if not already present. This means, that you can define the error message by yourself, if you attach the NotEmpty
validator in your input filter config. The standard message is defined in the NotEmpty
validator as NotEmpty::IS_EMPTY
constant.
return [
'input_filter_specs' => [
'foobar' => [
[
'name' => 'name',
'required' => true,
'filters' => [
[
'name' => StringTrim::class,
'options' => [],
],
],
'validators' => [
[
'name' => NotEmpty::class,
'options' => [
'messages' => [
NotEmpty::IS_EMPTY => 'Your message here',
],
],
],
],
'allow_empty' => false,
'continue_if_empty' => false,
],
],
],
];
In the options of the NotEmpty
validator, you can define the messages you want to show up on failure.
Another way could be the translator of the NotEmpty
validator. If you use a translation for your application, you can set your individual phrase for the error message. In this case you don 't have to mention the NotEmpty
validator in your input filter specification.