至少选中一个复选框Symfony2

Bar*_*rno 5 forms checkbox symfony

我在表单中添加以下字段:

->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');
                },
Run Code Online (Sandbox Code Playgroud)

我希望只有在选中至少一个复选框时才发送表单,如果可能的话,还有一个告诉用户的工具提示: at least one option must be selected

Eln*_*mov 9

尝试将Count约束放在持有集合的字段上:

use Symfony\Component\Validator\Constraints\Count;

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

    // ...
}
Run Code Online (Sandbox Code Playgroud)