4

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;

1 Answer 1

8

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

Sign up to request clarification or add additional context in comments.

7 Comments

I already read this, there is no answer to do this with an ArrayCollection.
Hi @Zwen2012 this validator have an options named traverse. From the doc: If this constraint is applied to a property that holds an array of objects, then each object in that array will be validated only if this option is set to true.
OK, can you please make an example? Ist it Like this? /** * @var ArrayCollection * @Assert\All({@Assert\Traverse(), * @Assert\Type(type="My\WebsiteBundle\Model\Checkout\Adult"), * }) */ protected $adults;
Hi @Zwen2012 the option is enabled by default so you don't need to specify other params. Check my update for an example. Hope this help (usually works for me..). Let me know
hi @Zwen2012 you are welcome! Initially i post the answer from mobile so i link only the doc reference without any sample.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.