title: Spring MVC form validation: how to make field optional?
body: <p>I have a form (Spring 3 MVC project) and I'm using DTO (data transfer object) for data validation. Data is sent to a controller and I'm checking for it's validity with <code>BindingResult.hasErrors()</code> method and appropriate annotations. I'm going to simplify here since I'm having a problem with numeric fields.</p>
  
  <p><strong>DTO:</strong></p>
  
  <pre><code>public class Item {
  
      private String discount;
  
      @Digits(integer = 15, fraction = 2)
      public String getDiscount() {
          return discount;
      }
  
  }
  </code></pre>
  
  <p>If I submit form with nothing written in <code>discount</code> field, <code>BindingResult.hasErrors()</code> will return true with message </p>
  
  <p><code>numeric value out of bounds (&lt;15 digits&gt;.&lt;2 digits&gt; expected)</code>. </p>
  
  <p>What I want to accomplish is that <code>discount</code> field can be empty but if something is written in it it should be in numerical format provided by <code>@Digits</code> annotation. How can I do that?</p>