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.

图片转代码服务由CSDN问答提供 功能建议

我在我的实体中配置了断言。 我创建了两种表单类型。 第一种,我有 name url 。 在第二个我有 description

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

当我尝试提交第一个表单时,它未经验证,因为 $ description 为空。

但是在第一种形式中,没有描述字段。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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']
        ));
    }
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题