1

I'm trying to validate a form in a symfony 2.3 project, So I have a 'Customer' field :

$builder
    ->add('customer', 
          'entity', 
           array('property'=> 'item',
                 'multiple' => true, 
                 'expanded' => true, 
                 'class' => 'OrdersBundle:Customer', 
                 'required' => true, 'empty_value' => '',
                 'query_builder' => function(\Ella\OrdersBundle\Repository\CustomerRepository $er) {
            return $er->createQueryBuilder('q')->andWhere("q.is_delete = 0")->orderBy('q.item', 'asc');
        }));

I'm trying to return an error when user didn't select anything, so i do this :

properties:
    customer: 
        - Choice: { min: 1, minMessage: 'message' }

Or

 properties:
    customer: 
        - NotBlank: 
            message: message

and other things, but nothing works , an idea of ​​what I'm doing wrong ?? In the doc they say we could use an array, but this doesn't work either ...

Actually Symfony return :

Either "choices" or "callback" must be specified on constraint Choice

Krishnak
  • 67
  • 1
  • 11

1 Answers1

4

For the Choice validator you either need to specify an array with the available allowed choices or a callback function, from the docs:

This constraint is used to ensure that the given value is one of a given set of valid choices. It can also be used to validate that each item in an array of items is one of those valid choices.

What you could use may be the Count validator:

customer:
        - Count:
            min: 1
            max: 99
            minMessage: "Min message"
            maxMessage: "You cannot specify more than {{ limit }}"
enricog
  • 4,226
  • 5
  • 35
  • 54
  • This didn't help me http://stackoverflow.com/questions/35766535/symfony2-choice-field-validation-not-working – Najki Mar 03 '16 at 08:16