至少选中一个复选框Symfony2

时间:2013-03-01 11:41:42

标签: forms symfony checkbox

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

->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');
                },

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

1 个答案:

答案 0 :(得分:8)

尝试将Count constraint放在包含该集合的字段上:

use Symfony\Component\Validator\Constraints\Count;

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

    // ...
}