Select Element Validation

2019-06-24 07:18发布

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?

3条回答
我想做一个坏孩纸
2楼-- · 2019-06-24 07:55

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.

查看更多
Melony?
3楼-- · 2019-06-24 08:00
    $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!' 
                    ),
                ),
            ),

        ),
    )));
查看更多
太酷不给撩
4楼-- · 2019-06-24 08:01

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

查看更多
登录 后发表回答