Select item check

I have a Select element in my form:

$this->add(array( 'name' => 'cat_id', 'type' => 'Zend\Form\Element\Select', 'options' => array( 'label' => 'Categoria', 'empty_option' => '', 'value_options' => array( '' => '', ), ), )); 

value_options populated with database information in my controller ... (is there a better way?)

And I have an InputFilter for it, 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'), ), ), ), )); 

Please note that I want to change the message isEmpty ... and what a 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 do I still have this message? Where did she come from? How can I change it?

Ps: It works well with text elements. Only with Select elements do I get this problem.

Additional question:

If I want to use InArray Validator, for example:

 array( 'name' => 'InArray', 'options' => array( 'haystack' => array( ... ), 'messages' => array( 'notInArray' => 'Valor nΓ£o encontrado no banco de dados' ), ), ), 

Do I need to always fill haystack fild? This is not to say that the validator should use the value_options form?

Tnks!

EDIT

I'm not sure because no one has confirmed this, but I believe that ZF2 does the default validation with the form element when you set it to type .

For instance,

 $this->add(array( 'name' => 'cat_id', 'type' => 'Zend\Form\Element\Select', 'options' => array( 'empty_option' => '', 'value_options' => array( // ... ), ), 'attributes' => array( // ... ), )); 

In this case, ZF2 tries to create some default checks for the Select element, for example, check if it is empty, and check the haystack.

So how can I override the messages in this check?

+4
source share
3 answers

There is a way to solve this problem. It should delete the default scan messages. Just add a line to the constructor for your form.

$ this-> setUseInputFilterDefaults (false);

+2
source

So, this is old, but I decided that I would give an answer, I hope, future searches in the future.

You have 'required' => true, as one of your options. This leads to the fact that validation will also occur on the field. In essence, this is the same as the NotEmpty validator for Select Elements.

I have not tested this, but I assume that if you delete 'required' => true and leave Validation NoEmpty, you will get a message.

0
source
  $inputFilter = new InputFilter(); $factory = new InputFactory(); $inputFilter->add($factory->createInput(array( 'name' => 'username', 'required' => true, 'filters' => array( array('name' => 'StripTags'), array('name' => 'StringTrim'), ), 'validators' => array( array( 'name' =>'NotEmpty', 'options' => array( 'messages' => array( \Zend\Validator\NotEmpty::IS_EMPTY => 'User name can not be empty.' ), ), ), array( 'name' => 'StringLength', 'options' => array( 'encoding' => 'UTF-8', 'min' => 4, 'max' => 20, 'messages' => array( 'stringLengthTooShort' => 'Please enter User Name between 4 to 20 character!', 'stringLengthTooLong' => 'Please enter User Name between 4 to 20 character!' ), ), ), ), ))); 
-1
source

Source: https://habr.com/ru/post/1444410/


All Articles