duanditang2916 2017-03-29 00:43
浏览 70

Symfony 3:在表单验证中忽略了GroupSequence验证组

I would like to validate a form using group validation and therefore I defined two validation groups. The second group should only be validated if the first group does not already cause violations. So I did this:

        $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();

But apparently none of the constraints are validated, the form is valid no matter what I enter in the email field.

What's wrong?

  • 写回答

1条回答 默认 最新

  • dongyun8138 2017-03-29 14:04
    关注

    According to the 3.2 documentation you don't need to initiate a new GroupSequence object under the validation_groups. Try this:

        $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();
    

    Reference: http://symfony.com/doc/current/form/validation_groups.html

    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等