title: how to return json encoded form errors in symfony
body: <p>I want to create a webservice to which I submit a form, and in case of errors, returns a JSON encoded list that tells me which field is wrong.</p>
  
  <p>currently I only get a list of error messages but not an html id or a name of the fields with errors</p>
  
  <p>here's my current code</p>
  
  <pre><code>public function saveAction(Request $request)
  {
      $em = $this-&gt;getDoctrine()-&gt;getManager();
  
      $form = $this-&gt;createForm(new TaskType(), new Task());
  
      $form-&gt;handleRequest($request);
  
      $task = $form-&gt;getData();
  
      if ($form-&gt;isValid()) {
  
          $em-&gt;persist($task);
          $em-&gt;flush();
  
          $array = array( 'status' =&gt; 201, 'msg' =&gt; 'Task Created'); 
  
      } else {
  
          $errors = $form-&gt;getErrors(true, true);
  
          $errorCollection = array();
          foreach($errors as $error){
                 $errorCollection[] = $error-&gt;getMessage();
          }
  
          $array = array( 'status' =&gt; 400, 'errorMsg' =&gt; 'Bad Request', 'errorReport' =&gt; $errorCollection); // data to return via JSON
      }
  
      $response = new Response( json_encode( $array ) );
      $response-&gt;headers-&gt;set( 'Content-Type', 'application/json' );
  
      return $response;
  }
  </code></pre>
  
  <p>this will give me a response like</p>
  
  <pre><code>{
  "status":400,
  "errorMsg":"Bad Request",
  "errorReport":{
          "Task cannot be blank",
          "Task date needs to be within the month"
      }
  }
  </code></pre>
  
  <p>but what I really want is something like</p>
  
  <pre><code>{
  "status":400,
  "errorMsg":"Bad Request",
  "errorReport":{
          "taskfield" : "Task cannot be blank",
          "taskdatefield" : "Task date needs to be within the month"
      }
  }
  </code></pre>
  
  <p>How can I achieve that?</p>