title: Spring MVC form validation
body: <p>my problem is that I have a form which has html select element with some choosing option value &amp; I want to validate those value using :</p>
  
  <pre><code>org.hibernate.validator.constraints
  or
  javax.validation.constraints
  </code></pre>
  
  <p>annotations. here is my select element:</p>
  
  <pre><code>&lt;select name="status" id="tbSelect"&gt;
      &lt;option value="ACTIVE"&gt;ACTIVE&lt;/option&gt;
      &lt;option value="LISTEN"&gt;LISTEN&lt;/option&gt;
      &lt;option value="DOWN"&gt;DOWN&lt;/option&gt;
  &lt;/select&gt;
  </code></pre>
  
  <p>how I can for example validate the value of the options(DOWN,LISTEN,ACTIVE) inside the select element by using the annotation validators which I mention above?</p>
  
  <p>my form is like this : </p>
  
  <pre><code>&lt;form:form action="../agents/add" method="POST" commandName="myAgent"&gt;
       &lt;form:select id="tbSelect" path="state"&gt;
              &lt;form:option value="ACTIVE" path="state"&gt;ACTIVE&lt;/form:option&gt;
              &lt;form:option value="LISTEN" path="state"&gt;LISTEN&lt;/form:option&gt;
              &lt;form:option value="DOWN" path="state"&gt;DOWN&lt;/form:option&gt;
      &lt;/form:select&gt;
  </code></pre>
  
  <p>I have defined my controller method  like this:</p>
  
  <pre><code>    @RequestMapping(value = "agents/add", method = RequestMethod.POST)
      public String addAgentSubmit(@ModelAttribute("myAgent") @Valid final AgentValidator agent, BindingResult result, RedirectAttributes redirect) {
        if (result.hasErrors()) {
          return "admin/agent/add";
        } 
         ...
      }
  </code></pre>
  
  <p>and I also define a ModelAttribute like this:</p>
  
  <pre><code>@ModelAttribute("myAgent")
   public AgentValidator getLoginForm() {
      return new AgentValidator();
   }
  </code></pre>
  
  <p>Here is my AgentValidator class also:</p>
  
  <pre><code>public class AgentValidator {
      @NotEmpty(message = "your state can not be empty !")
      private String state;
  </code></pre>