Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony: How to use Assert/Validation for ArrayCollection

In Symfony 3, its not allowed to use cascade_validation anymore. So you have to make an Assert for the types. But it doesn't work, the form is valid even when a field is BLANK but the Assert said NOtBlank. I have a class Participants and I want to check the Adults ArrayCollection when checking my participant Model.

//Participant Model
    /**
         * @var ArrayCollection
         * @Assert\All({
         *     @Assert\Type(type="My\WebsiteBundle\Model\Adult"),
         * })
         */
        protected $adults;

//Adult Model
    class Adult
    {
        /**
         * @var string
         *
     * @Assert\NotBlank()
     */
    protected $salutation;

    /**
     * @var string
     *
     * @Assert\NotBlank()
     */
    protected $firstname;

    /**
     * @var string
     *
     * @Assert\NotBlank()
     */
    protected $lastname;
like image 658
Zwen2012 Avatar asked Mar 10 '23 13:03

Zwen2012


1 Answers

You should use the Valid assetion as described here http://symfony.com/doc/current/reference/constraints/Valid.html in the doc

As example:

    /**
     * @var ArrayCollection
     *
     * @Assert\All({
     *     @Assert\Type(type="My\WebsiteBundle\Model\Adult"),
     * })
     * @Assert\Valid
     */
    protected $adults;

Hope this help

like image 128
Matteo Avatar answered Apr 08 '23 14:04

Matteo