title: Java spring framework form validation
body: <p>I am using spring framework 3.0.5.</p>
  
  <p>I have a form:</p>
  
  <pre><code>&lt;form:form method="POST" modelAttribute="postedEntry"&gt;
      Category: &lt;form:select path="category" items="${menus}" itemValue="id" itemLabel="menuName"/&gt;&lt;form:errors path="category"/&gt;
      &lt;br/&gt;Title: &lt;form:input path="title"/&gt;&lt;form:errors path="title"/&gt;
      &lt;br/&gt;Short Description: &lt;form:textarea path="shortDesc"/&gt;&lt;form:errors path="shortDesc"/&gt;
      &lt;br/&gt;Body: &lt;form:textarea path="body"/&gt;&lt;form:errors path="body"/&gt;
      &lt;br/&gt;&lt;input type="submit" value="POST IT!11"/&gt;
  &lt;/form:form&gt;
  </code></pre>
  
  <p>And a domain class Entry:</p>
  
  <pre><code>public class Entry {
      private int id;
      private int category;
      private String title;
      private String shortDesc;
      private String body;
      private Date date;
  //getters and setters
  }
  </code></pre>
  
  <p>And a controller:</p>
  
  <pre><code>@RequestMapping(value="/entryPost",method=RequestMethod.POST)
  public String entryPost(@ModelAttribute("postedEntry") Entry entry,BindingResult result){
      entryValidator.validate(entry, result);
      if(result.hasErrors()){
          return "entryPost";
      }else{
          rusService.postEntry(entry);
          return "redirect:entryPost";
      }
  }
  </code></pre>
  
  <p>And in my service object:</p>
  
  <pre><code>public void postEntry(final Entry entry){
      String sql = "INSERT INTO entries (category,title,shortDesc,body,date) VALUES(?,?,?,?,?)";
      jdbcTemplate.update(sql,new Object[]{entry.getCategory(),entry.getTitle(),entry.getShortDesc(),entry.getBody(),entry.getDate()});
  }
  </code></pre>
  
  <p>And a validator:</p>
  
  <pre><code>public class EntryValidator implements Validator{
      public boolean supports(Class clazz){
          return Entry.class.isAssignableFrom(clazz);
      }
      public void validate(Object target,Errors errors){
          ValidationUtils.rejectIfEmptyOrWhitespace(errors, "category", "required.category","Category is required!");
          ValidationUtils.rejectIfEmptyOrWhitespace(errors, "title", "required.title", "Title is required!");
          ValidationUtils.rejectIfEmptyOrWhitespace(errors, "shortDesc","required.shortDesc", "Short description is required!");
          ValidationUtils.rejectIfEmptyOrWhitespace(errors, "body", "required.body", "Body is required!");
          Entry entry = (Entry) target;
          entry.setDate(new Date());
      }
  }
  </code></pre>
  
  <p>If somebody sends the string value of a category, not integer, it will type near the form:
  Failed to convert property value of type java.lang.String to required type int for property category; nested exception is org.springframework.core.convert.ConversionFailedException: Unable to convert value "h" from type java.lang.String to type int; nested exception is java.lang.NumberFormatException: For input string: "h" </p>
  
  <p>But I don't want that it typed this. How can I somehow validate this value and throw my own message?
  I tried to check it in my validator by adding:</p>
  
  <pre><code>int cat = entry.getCatrgory();
  if(cat.equals("0"))
      errors.reject("invalid.category","The category is invalid!");
  </code></pre>
  
  <p>But it didn't work - it still threw ConversionFailedException exception.</p>