title: Get all errors along with fields the error is connected to
body: <p>I'm using Symfony2 forms to validate POST and PUT requests to an API. The form handles binding the request data to the underlying entity and then validating the entity. Everything is working pretty well except for collecting errors. I'm using the FOSRestBundle and am throwing a Symfony\Component\HttpKernel\Exception\HttpException with a 400 status code and a message containing the form error messages if validation fails. The FOSRestBundle handles converting this into a JSON response. The controller method I have to perform all of this looks like the following (all fields bubble their errors up to the form):</p>
  
  <pre><code>protected function validateEntity(AbstractType $type, $entity, Request $request)
  {
      $form = $this-&gt;createForm($type, $entity);
      $form-&gt;bind($request);
      if (! $form-&gt;isValid()) {
          $message = ['Invalid parameters passed.'];
          foreach ($form-&gt;getErrors() as $error) {
              $message[] = $error-&gt;getMessage();
          }
          throw new HttpException(Codes::HTTP_BAD_REQUEST, implode("\n", $message));
      }
  }
  </code></pre>
  
  <p>The problem I have is that when I collect the form level errors through $form->getErrors() I can only access the error message and not the name of the field that the error is connected to. This is a particular problem when a POST or PUT parameter corresponds to an id for a related entity. If an invalid value is submitted the error messages for that is simply 'This value is not valid', which is not very good in this context. Ideally I'd like to do either of the following:</p>
  
  <ul>
  <li>For each error get hold of the field name it's connected to so that I can format the message something like "fieldname: error message"</li>
  <li>If that's not possible is it possible to customise the error message for an invalid entity type so that something better than 'This value is not valid' is displayed?</li>
  </ul>