空值传递给Zend Framework 2验证器 [英] Empty values passed to Zend framework 2 validators
问题描述
如何通过Zend Framework 2 ValidatorChain将空值传递给自定义验证器?
How can I pass an empty value through Zend framework 2 ValidatorChain to my custom validator?
ZF1上的allowEmpty(false)
在ZF2上具有空元素值:
On ZF2 with empty element value :
-
如果为
allowEmpty = false
,则将NotEmptyValidator与breakOnFailure = true
,@see Zend/InputFilter/Input#injectNotEmptyValidator
添加到ValidatorChain的顶部.
If
allowEmpty = false
, NotEmptyValidator is added to the top of ValidatorChain withbreakOnFailure = true
,@see Zend/InputFilter/Input#injectNotEmptyValidator
.
如果allowEmpty = true
,则将Element视为有效,@see Zend/InputFilter/BaseInputFilter#isValid
If allowEmpty = true
, Element is considered as Valid, @see Zend/InputFilter/BaseInputFilter#isValid
if ($input->allowEmpty()) {
$this->validInputs[$name] = $input;
continue;
}
推荐答案
以下适用于ZF2 2.1.1版:
Following works for ZF2 version 2.1.1:
问题(如果我正确理解的话)是在下面的示例中,对于'fieldName'
的空值,没有触发验证.尽管在
The problem (if I got it correctly) is that in following example, for empty values of 'fieldName'
, no validation is triggered. This can be quite annoying, though in
$input = new \Zend\InputFilter\Input('fieldName');
$input
->setAllowEmpty(true)
->setRequired(false)
->getValidatorChain()
->attach(new \Zend\Validator\Callback(function ($value) {
echo 'called validator!';
return true; // valid
}));
$inputFilter = new \Zend\InputFilter\InputFilter();
$inputFilter->add($input);
$inputFilter->setData(array('fieldName' => 'value'));
var_dump($inputFilter->isValid()); // true, echoes 'called validator!'
$inputFilter->setData(array('fieldName' => ''));
var_dump($inputFilter->isValid()); // true, no output
$inputFilter->setData(array());
var_dump($inputFilter->isValid()); // true, no output
遇到特殊情况时,这很烦人,例如检查分配给CMS中页面的URL并避免冲突(空URL仍然是URL!).
This is quite annoying when you have particular cases, like checking an URL assigned to a page in your CMS and avoiding collisions (empty URL is still an URL!).
有一种处理空字符串的方法,它基本上是自己附加NotEmpty
验证程序,并避免调用setRequired
和setAllowEmpty
.这基本上会告诉 Zend\InputFilter\Input#injectNotEmptyValidator
请勿自动将NotEmpty
验证程序附加到其上:
There's a way of handling this for empty strings, which is to basically attach the NotEmpty
validator on your own, and avoiding calls to setRequired
and setAllowEmpty
. This will basically tell Zend\InputFilter\Input#injectNotEmptyValidator
not to utomatically attach a NotEmpty
validator on its own:
$input = new \Zend\InputFilter\Input('fieldName');
$input
->getValidatorChain()
->attach(new \Zend\Validator\NotEmpty(\Zend\Validator\NotEmpty::NULL))
->attach(new \Zend\Validator\Callback(function ($value) {
echo 'called validator!';
return true; // valid
}));
$inputFilter = new \Zend\InputFilter\InputFilter();
$inputFilter->add($input);
$inputFilter->setData(array('fieldName' => 'value'));
var_dump($inputFilter->isValid()); // true, echoes 'called validator!'
$inputFilter->setData(array('fieldName' => ''));
var_dump($inputFilter->isValid()); // true, echoes 'called validator!'
$inputFilter->setData(array());
var_dump($inputFilter->isValid()); // false (null was passed to the validator)
如果您还想检查null
,则需要扩展Zend\InputFilter\Input
,如下所示:
If you also want to check against null
, you will need to extend Zend\InputFilter\Input
as following:
class MyInput extends \Zend\InputFilter\Input
{
// disabling auto-injection of the `NotEmpty` validator
protected function injectNotEmptyValidator() {}
}
这篇关于空值传递给Zend Framework 2验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!