I have a Select element in my form like this:
$this->add(array(
'name' => 'cat_id',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => 'Categoria',
'empty_option' => '',
'value_options' => array(
'' => '',
),
),
));
The value_options
is filled with database info in my controller... (there's is a better way?)
And I have a InputFilter for him, like this:
$this->add(array(
'name' => 'cat_id',
'required' => true,
'validators' => array(
array(
'name' => 'NotEmpty',
'break_chain_on_failure' => true,
'options' => array(
'messages' => array('isEmpty' => 'O campo "Categoria" é obrigatório'),
),
),
),
));
Note that I want to change the isEmpty
message... and that's the problem!
When I submit the form I still get the same message in english:
cat_id:
isEmpty : Value is required and can't be empty
So my question is : Why I still have this message? Where it came from? How can I change it?
Ps.: With Text elements, it works well. Only with Select elements I get this issue.
Extra question :
If I wants to use an InArray Validator, like this:
array(
'name' => 'InArray',
'options' => array(
'haystack' => array( ... ),
'messages' => array(
'notInArray' => 'Valor não encontrado no banco de dados'
),
),
),
I need always to fill the haystack
fild? There's no way that say to the validator to use the form value_options
?
Tnks!
EDIT
I'm not sure, cause nobody had confirmed that, but I guess ZF2 make default validations to an form element when you set the type
for it.
For exemple,
$this->add(array(
'name' => 'cat_id',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'empty_option' => '',
'value_options' => array(
// ...
),
),
'attributes' => array(
// ...
),
));
In this case, ZF2 looks to create some default validations to an Select element, like check if is empty and check the haystack.
So how can I override the messages in this validation?
So this is old, but I figured I'd give an answer to hopefully help future searches.
You have
'required' => true,
as one of your options. This causes a validation to happen on the field also. Essentially the same as the NotEmpty validator for Select Elements.I haven't tested this, but I'm guessing if you remove the
'required' => true
and leave the NoEmpty validator you will get your message.There is a way to solve this problem. It should remove the default validation messages. Just add the line in the constructor for your form.
$this->setUseInputFilterDefaults(false);