Search code examples
formsvalidationsymfonymultiple-choice

correct usage of "min" within validating a symfony choice form


As described under the book http://symfony.com/doc/current/reference/constraints/Choice.html#min I would like to use the "min" options to validate a choice with must have at least one selected checkbox in it

The form looks like

->add('usergroups', 'entity', array('class' => 'PrUserBundle:Group','property' => 'name','required' => true, 'multiple'  => true, 'expanded'  => true))

My validation.yml looks liek:

Pr\UserBundle\Entity\User:
constraints:
//...
properties:
    //.....
    locations:
        - Length:
            min: 7 { message: "Please select at least one group." }

I am totally wrong doing this, but the book won't tell me any more helpful so I ask you. OR do I have to use True/False?


Solution

  • You are trying to use the Length constraint rather than the choice.

    You should be using it like..

    Pr\UserBundle\Entity\User:
        constraints:
        //...
        properties:
            //.....
            locations:
                - Choice:
                    min: 7
                    minMessage: "Please select at least one group."
    

    All of the options that are featured on that page would be in a single level array like

    - Choice: { min: 7, minMessage: 'message' }
    

    or

    - Choice:
        min: 7
        minMessage: 'message'