0

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?

TheTom
  • 934
  • 2
  • 14
  • 40

1 Answers1

1

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'
qooplmao
  • 17,622
  • 2
  • 44
  • 69
  • Cool, that exactly what I want to use. Can I also use Count? Its nearly the same isn't it? – TheTom Aug 08 '14 at 07:21
  • 1
    Yeah, as far as I know. I would generally (and possibly incorrectly) go for a choice and count constraints (for specifying options and count, respectively) just because it's more obvious to me what I'm validating against on each part at a later date. – qooplmao Aug 08 '14 at 08:15
  • well, I think count is my favorite option :) It works perfectly. thx a lot. – TheTom Aug 08 '14 at 09:11
  • This didn't help me http://stackoverflow.com/questions/35766535/symfony2-choice-field-validation-not-working – Najki Mar 03 '16 at 08:16