2015-02-10 83 views
1

我想在symfony的2.3項目來驗證一個表單, 所以我有一個「客戶」字段:驗證Symfony2的實體選擇現場

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

我試圖返回一個錯誤,當用戶沒「T選擇什麼,所以我這樣做:

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

或者

properties: 
    customer: 
     - NotBlank: 
      message: message 

和其他東西,但ñ沒有工作,我做錯什麼的想法? 在他們說,我們可以使用一個數組,但這也不管用的文檔...

其實Symfony的回報:

無論是「選擇」或「回調」必須在約束選擇指定

回答

4

對於Choice驗證您可能需要指定與可允許的選擇,或者一個回調函數數組,從文檔:

該約束用於確保給定值是給定的一組有效選擇之一。它也可以用來驗證項目數組中的每個項目是那些有效的選擇之一。

你可以使用什麼可能是Count驗證:

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

謝謝!它完美的工作! – Krishnak 2015-02-10 15:32:42

+0

這並沒有幫助我http://stackoverflow.com/questions/35766535/symfony2-choice-field-validation-not-working – Najki 2016-03-03 08:16:18