验证Symfony2实体选择字段

时间:2015-02-10 14:43:50

标签: php validation symfony

我试图在symfony 2.3项目中验证表单, 所以我有一个'客户'领域:

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

我试图在用户没有选择任何内容时返回错误,所以我这样做:

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

 properties:
    customer: 
        - NotBlank: 
            message: message

和其他的东西,但没有任何作用,一个关于我做错了什么的想法? 在文档中,他们说我们可以使用数组,但这不起作用......

实际上Symfony回归:

  

选择"选择"或者"回调"必须在约束条件

上指定

1 个答案:

答案 0 :(得分:4)

对于Choice验证程序,您需要从文档中指定具有可用允许选项的数组或回调函数:

  

此约束用于确保给定值是给定的一组有效选择之一。它还可用于验证项目数组中的每个项目是否为这些有效选项之一。

您可以使用Count验证器:

customer:
        - Count:
            min: 1
            max: 99
            minMessage: "Min message"
            maxMessage: "You cannot specify more than {{ limit }}"