title: Spring: How to resolve a validation error -> error code -> error message
body: <p>In Spring, after validation we get a <code>BindingResult</code> object in the <code>controller</code>.</p>
  
  <p>Simple enough, if I get validation errors I want to re-display my form with the error message above each afflicted field.</p>
  
  <p>So to check for field errors on field <code>username</code> of my <code>FormObject</code> I call:</p>
  
  <pre><code>FieldError usernameFieldError = bindingResult.getFieldError("username");
  </code></pre>
  
  <p>Great, now I hold a <code>FieldError</code> object which, assuming I'm using the <code>DefaultMessageCodeResolver</code> now contains something like 4 possible error codes.</p>
  
  <p><strong>How do I go from <code>FieldError</code> -> A string that is consumable to the user?</strong></p>
  
  <p>I have a <code>MessageSource</code> defined in my webapplication context, so I can map a single error code to a message.</p>
  
  <p>But sometimes the default message will be best, and sometimes I expect that two of the error codes might have a relevant message, so we need to choose the best one. </p>
  
  <p>What method do I use to determine the <strong>best</strong> possible error message to present for a field error?</p>
  
  <ul>
  <li>Do I need to write some algorithm to go through all the error codes and pick from the most specific?</li>
  <li>Does spring provide any support for helping determine the most specific error message?</li>
  <li>This whole process seems so long and convoluted, I thought spring was supposed to make this stuff easy. Maybe I'm totally off base somehow?</li>
  </ul>