dream8877 2016-03-18 17:18
浏览 115
已采纳

在表单类型步骤Symfony中禁用断言到自定义字段

I have assert configured in my entity. I created two forms types. In the first, I have name and url. In the second I have description.

/*
*@Assert/NotBlank()
*/
$description;

When I try to submit the first form, it is not validated because the $description is blank.

But in the first form, not have the description field.

  • 写回答

1条回答 默认 最新

  • douxunwei8259 2016-03-18 17:34
    关注

    What you are looking for are validation groups (http://symfony.com/doc/current/book/validation.html#validation-groups and http://symfony.com/doc/current/book/forms.html#validation-groups).

    Basically for your case you need to define two validation groups and set each one of them on your form types.

    YourEntity

    /**
     * @Assert\NotBlank(groups={"FirstForm"})
     */
    private $name;
    
    /**
     * @Assert\NotBlank(groups={"FirstForm"})
     */
    private $url;
    
    /**
     * @Assert\NotBlank(groups={"SecondForm"})
     */
    private $description;
    

    FirstFormType

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'validation_groups' => ['Default', 'FirstForm']
        ));
    }
    

    SecondFormType

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'validation_groups' => ['Default', 'SecondForm']
        ));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog