在我的Zend Framework 2 Form类中,我添加了我的元素:
$this->add(array(
'name' => 'passwordConfirm',
'type' => 'Password',
'attributes' => array(
'required' => 'required',
'placeholder' => 'Confirm password',
),
'options' => array(
'label' => 'Confirm password',
'column-size' => 'sm-10',
'label_attributes' => array(
'class' => 'col-sm-2',
),
),
'validators' => array(
array(
'name' => 'NotEmpty',
),
array(
'name' => 'Identical',
'options' => array(
'token' => 'password',
),
),
),
));
正如official reference中所述。我像这样创建一个新的表单实例:
$form = $this->getServiceLocator()->get('user.auth.form');
$hydrator = new DoctrineHydrator($this->entityManager());
$form->setHydrator($hydrator);
$form->bind($user);
但是,validators
未添加到元素验证器链中。这是var_dump($form->get('passwordConfirm'));
object(Zend\InputFilter\Input)[506]
protected 'allowEmpty' => boolean true
protected 'continueIfEmpty' => boolean false
protected 'breakOnFailure' => boolean false
protected 'errorMessage' => null
protected 'filterChain' =>
object(Zend\Filter\FilterChain)[507]
protected 'plugins' => null
protected 'filters' =>
object(Zend\Stdlib\PriorityQueue)[508]
protected 'queueClass' => string 'Zend\Stdlib\SplPriorityQueue' (length=28)
protected 'items' =>
array (size=0)
...
protected 'queue' => null
protected 'options' =>
array (size=0)
empty
protected 'name' => string 'passwordConfirm' (length=15)
protected 'notEmptyValidator' => boolean false
protected 'required' => boolean false
protected 'validatorChain' =>
object(Zend\Validator\ValidatorChain)[509]
protected 'plugins' => null
protected 'validators' =>
array (size=0)
empty
protected 'messages' =>
array (size=0)
empty
protected 'value' => null
protected 'fallbackValue' => null
protected 'hasFallback' => boolean false
当我为验证器输入无效输入时,$form->isValid()
接受表单没有问题。
我做错了什么?
答案 0 :(得分:0)
在查看源代码并使用此语法尝试其他一些内容之后,我发现从Zend Framework 2.3开始,您无法使用此数组语法添加验证程序。我上面引用的官方文档中的例子是完全错误的。
要进行验证,请使用参考中Zend\Validator
部分中描述的更精细的语法。