2

I have a validator on a form entry like this :

    $this->add(array(
        'name' => 'email',
        'required' => true,
        'filter' => array(
            'name' => 'StripTags',
        ),
        'validators' => array(
            array(
                'name' => 'NotEmpty',
                'options' => array(
                    'messages' => array(
                        \Zend\Validator\NotEmpty::IS_EMPTY => 'Veuillez renseigner une adresse e-mail.',
                    ),
                ),
            ),                
            array(
                'name' => 'StringLength',
                'options' => array(
                    'encoding' => 'UTF-8',
                    'min' => 1,
                    'max' => 100,
                ),
            ),
            array(
                'name' => 'EmailAddress',
                'options' => array(
                ),
            ),
        ),
    ));

There's basicaly 3 validators on my input. The NotEmpty, the StringLength, and the EmailAdress.

Is there any way to set a kind of priority between them ? Right now, if I submit an empty form, I get messages relative to those 3 validators, ie. :

  • My input is empty.
  • My string length is too short (thanks...)
  • My input is not an email (thanks again...)

Is there anyway to tell my validator to stop at the first failure ? (or at least to only print the 1st message).

4

1 回答 1

5

'break_chain_on_failure'在验证器规范中使用值为 true的密钥,即

$this->add(array(
    'name' => 'email',
    'required' => true,
    'filter' => array(
        'name' => 'StripTags',
    ),
    'validators' => array(
        array(
            'name' => 'NotEmpty',
            'break_chain_on_failure' => true,
            'options' => array(
                'messages' => array(
                    \Zend\Validator\NotEmpty::IS_EMPTY => 'Veuillez renseigner une adresse e-mail.',
                ),
            ),
        ),                
        array(
            'name' => 'StringLength',
            'break_chain_on_failure' => true, 
            'options' => array(
                'encoding' => 'UTF-8',
                'min' => 1,
                'max' => 100,
            ),
        ),
        array(
            'name' => 'EmailAddress',
            'options' => array(
            ),
        ),
    ),
));
于 2013-05-30T16:34:19.603 回答