我想使用组验证来验证表单,因此我定义了两个验证组。仅当第一组尚未导致违规时,才应验证第二组。所以我这样做了:
$form = $this->createFormBuilder(null,array('validation_groups' => new GroupSequence(array('group1','group2'))))
->add('email', EmailType::class,array('constraints' => array(
new Assert\Email(array('groups' => array('group1'))),
new Assert\NotBlank(array('groups' => array('group1'))),
new CustomAssert\AlreadyRegistered(array('groups' => array('group2'))))))
->add('submit',SubmitType::class,array())
->getForm();
但显然没有任何约束被验证,无论我在电子邮件领域输入什么,表格都是有效的。
怎么了?
答案 0 :(得分:0)
根据3.2文档,您不需要在GroupSequence
下发起新的validation_groups
对象。试试这个:
$form = $this->createFormBuilder(null, array(
'validation_groups' => array('group1','group2')
))
->add(
'email',
EmailType::class,
array(
'constraints' => array(
new Assert\Email(array('groups' => array('group1'))),
new Assert\NotBlank(array('groups' => array('group1'))),
new CustomAssert\AlreadyRegistered(array('groups' => array('group2')))
)))
->add(
'submit',
SubmitType::class
)
->getForm();
参考:http://symfony.com/doc/current/form/validation_groups.html