Zend framework2 Zend framework 2如何在字段集中使用验证程序链
我需要在字段集的getInputFilterSpecification方法中使用验证器链,以使用breakChainOnFailure参数并仅获取一条错误消息 我知道使用InputFilter类制作验证程序链如何解释 但是我想用工厂格式做同样的。 在此示例中,我可以将breakChainOnFailure参数放在何处:Zend framework2 Zend framework 2如何在字段集中使用验证程序链,zend-framework2,zend-validate,zend-inputfilter,Zend Framework2,Zend Validate,Zend Inputfilter,我需要在字段集的getInputFilterSpecification方法中使用验证器链,以使用breakChainOnFailure参数并仅获取一条错误消息 我知道使用InputFilter类制作验证程序链如何解释 但是我想用工厂格式做同样的。 在此示例中,我可以将breakChainOnFailure参数放在何处: $factory = new Factory(); $inputFilter = $factory->createInputFilter(array(
$factory = new Factory();
$inputFilter = $factory->createInputFilter(array(
'password' => array(
'name' => 'password',
'required' => true,
'validators' => array(
array(
'name' => 'not_empty',
),
array(
'name' => 'string_length',
),
),
),
));
要回答您的问题,我们需要查看InputFilter工厂,在那里我们可以找到。正如您所看到的,对于验证器,它在规范中寻找一个
break\u chain\u on\u failure
键。您只需要将它添加到验证器规范数组中
$factory = new Factory();
$inputFilter = $factory->createInputFilter(array(
'password' => array(
'name' => 'password',
'required' => true,
'validators' => array(
array(
'name' => 'not_empty',
'break_chain_on_failure' => true,
),
array(
'name' => 'string_length',
),
),
),
));
顺便说一下,
FilterChain
()和ValidatorChain
()的attachByName
方法签名是不同的。在第一个示例中,您在一个过滤器链上调用该方法,该过滤器链根本不支持故障时中断。(您可能还注意到,它是validator chain方法的第三个参数,而不是第二个)回顾我的代码,我需要使用验证程序类实例(而不是工厂规范)在验证链规范中使用break_chain_on_failure参数
查看示例:
'password' = array(
'required' => true,
'validators' => array(
new NotEmpty(), //these are validator instace classes
new HostName(), //and them may be declared before
),
);
非常感谢你的帮助。您是对的,我的attachByName方法示例被修改为执行该示例。我还以为是一样的。
'password' = array(
'required' => true,
'validators' => array(
new NotEmpty(), //these are validator instace classes
new HostName(), //and them may be declared before
),
);