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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失