2017-08-02 3 views
1

私はコレクションタイプ(フィールドcol)を含むフォームを持っています。symfonyのすべてのフォームエラーを表示

コレクションタイプのフィールド(aと呼ぶ)では、エラーを返すバリデータがあります。

+0

あなたは確かにあなたのバリデータを渡していますか? –

+0

コントローラコードを表示できますか?オブジェクトを検証するだけでは、エラーは発生しません。フォームを検証する必要があります。 http://symfony.com/doc/current/validation.html#using-the-validator-service – Shefali

+0

'{{form_rest(form)}}'でエラーをレンダリングするため、バリデーターでエラーが返される –

答えて

0

...私は{{form_errors(form)}}{{form_errors(form.col)}}と試みるが、彼らは働いていない私は、通常の操作を行います。

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

'{ {form_errors(form.col)}} 'はエラーを表示しません(私の質問を参照) –

+0

' form.col.vars.errors'を試してみてください –

0

あなたはすべてのエラーを取得し、JSON配列としてそれを返すためにFormErrorsSerializerを使用することができ、このコードは私がそれに取り組んでいるプロジェクトからスナップします

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 
関連する問題