ZF2:InputFilter中验证程序的优先级 [英] ZF2 : Priority for Validators in InputFilter

查看:86
本文介绍了ZF2:InputFilter中验证程序的优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这样的表单项上有一个验证器:

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

我的输入中基本上有3个验证器. NotEmptyStringLengthEmailAdress.

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. :

  • 我的输入为空.
  • 我的字符串长度太短(谢谢...)
  • 我输入的不是电子邮件(再次感谢...)

有没有告诉我的验证器在第一次失败时停止? (或至少仅打印第一条消息).

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

推荐答案

在验证器规范中使用'break_chain_on_failure'键,其值为true,即

Use the 'break_chain_on_failure' key in your validator spec with a value of true, ie

$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(
            ),
        ),
    ),
));

这篇关于ZF2:InputFilter中验证程序的优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆