在ZendFramework2Form类中,我添加了如下元素:
$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',
),
),
),
));正如正式参考中所描述的那样。我创建了一个新的表单实例,如下所示:
$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()接受,而不会出现问题。
我做错什么了?
发布于 2014-04-14 08:06:33
在查看了源代码并使用此语法尝试了一些其他内容之后,我发现您无法在ZendFramework2.3中使用此数组语法添加验证器。我上面引用的正式文档中的例子完全是错误的。
为了进行验证,请使用引用中关于Zend\Validator的部分中描述的更详细的语法。
https://stackoverflow.com/questions/22787564
复制相似问题