Symfony2 - Validation not working for embedded Form Type

后端 未结 8 1364
北海茫月
北海茫月 2020-12-07 12:59

I have a form that combines two entities (User and Profile).

Validation seems to work on the first part of the form that comes form the User Entity and is the basis

相关标签:
8条回答
  • 2020-12-07 13:35

    According to form type documentation you can also use Valid constraint instead of cascade_validation option.

    $builder->add('children', 'collection', array(
        'type'        => new ChildType(),
        'constraints' => array(new Valid()),
    ));
    

    Example from the owner entity:

    /**
     * @var Collection
     *
     * @ORM\OneToMany(targetEntity="Child", ...)
     * @Assert\Valid()
     */
    private $children
    
    0 讨论(0)
  • 2020-12-07 13:38

    I was looking for the exact same thing and here is what I found

    http://symfony.com/doc/master/book/forms.html#forms-embedding-single-object

    You need to tell the main entity to validate it's sub entities like so:

    /**
     * @Assert\Type(type="AppBundle\Entity\Category")
     * @Assert\Valid()
     */
     private $subentity;
    

    I've tested this on symfony 2.8 and it works.

    0 讨论(0)
提交回复
热议问题