Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

at least one checkbox selected Symfony2

I am adding the following field to my form:

->add('interessi_profilo', 'entity', array( 
                'label' => 'Interessi (Tr)',
                'class' => 'MyProfiloBundle:TipoInteresse',
                'required'  => true,
                'multiple' => true,
                'expanded'  => true,
                'property' => 'tipo',
                'query_builder' => function(\My\ProfiloBundle\Entity\TipoInteresseRepository $er) {
                    return $er->createQueryBuilder('u')
                            ->orderBy('u.id', 'ASC');
                },

I would like for the form to be sent only if at least one checkbox is selected and, if possible, have a tooltip that tells the user: at least one option must be selected

like image 711
Barno Avatar asked Mar 01 '13 11:03

Barno


1 Answers

Try putting the Count constraint on the field holding the collection:

use Symfony\Component\Validator\Constraints\Count;

class Entity 
{
    /**
     * @Count(min = 1, minMessage = "At least one item must be selected")
     */
    private $collection;

    // ...
}
like image 63
Elnur Abdurrakhimov Avatar answered Nov 10 '22 16:11

Elnur Abdurrakhimov