2017-08-02 54 views
1

我有一個包含collectiontype(字段col)的表單。顯示所有在twig symfony中的表格錯誤

在集合類型的字段(稱爲a),我有驗證器返回錯誤。

我嘗試用{{form_errors(form)}}{{form_errors(form.col)}}但他們沒有工作...

+0

你確定你在你驗證過? –

+0

你能顯示你的控制器代碼嗎?如果你只是驗證對象,那麼它不會給你錯誤。你必須驗證你的表單。 http://symfony.com/doc/current/validation.html#using-the-validator-service – Shefali

+0

由驗證器返回的錯誤正常工作,因爲{{form_rest(表單)}}'呈現輸入錯誤 –

回答

0

我通常做的:

{% if form.col.vars.errors|length %} 
<span> 
    {{ form.col.vars.errors }} 
</span> 
{% endif %} 
+0

'{{ {form_errors(form.col)}}'不顯示錯誤(看我的問題) –

+0

試試'form.col.vars.errors' –

0

可以使用FormErrorsSerializer來獲取所有錯誤,並返回它作爲JSON陣列,從項目我工作的此代碼剪斷

function addOwnership(Request $request) 
{ 
    $id = $request->get('land_id'); 
    $land = $this->getDoctrine()->getRepository('DamanBundleCoreBundle:Land')->find($id); 
    $ownership = new OwnershipHistory(); 
    $ownership_form = $this->createForm('Daman\Bundle\CoreBundle\Form\OwnershipType', $ownership); 
    $ownership_form->handleRequest($request); 

    if ($ownership_form->isSubmitted()) { 
     if ($ownership_form->isValid()) { 

      $em = $this->getDoctrine()->getManager(); 
      $ownership->setLandId($land); 
      $em->persist($ownership); 
      $em->flush(); 
      $response = array('success' => true, 
       'msg' => $this->get('translator')->trans('data_has_been_successfully_added'), 
       'id' => $ownership->getId(), 
       'action' => 'refresh' 
      ); 

     } else { 
      $errors = $this->get('form_serializer')->serializeFormErrors($ownership_form, true, false); 
      $response = array('success' => false, 'msg' => $this->get('translator')->trans('error_exist_in_the_form'), 'errors' => $errors); 

     } 
     $jsonResponse = new JsonResponse($response); 
     return $jsonResponse; 

    } 


}//end function