Select Element Validation

≡放荡痞女 提交于 2019-12-10 17:12:57

问题


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?


回答1:


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);




回答2:


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.




回答3:


    $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!' 
                    ),
                ),
            ),

        ),
    )));


来源:https://stackoverflow.com/questions/13255535/select-element-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!