title: Custom Validator Annotation in Spring MVC
body: <p><br/>
  I created a custom validation to <code>&lt;form:select&gt;</code> that populate country list.</p>
  
  <p><br/>
  <strong>Customer.jsp</strong></p>
  
  <pre><code>    Country: 
      &lt;form:select path="country" items="${countries}" /&gt;
      &lt;form:errors path="country" cssClass="error"/&gt;
  </code></pre>
  
  <p><strong>FomeController.java</strong></p>
  
  <pre><code>    @RequestMapping(value = "/customer", method = RequestMethod.POST)
      public String prosCustomer(Model model,
              @Valid @ModelAttribute("defaultcustomer") Customer customer,
              BindingResult result
      ) {
          CustomerValidator vali = new CustomerValidator();
          vali.validate(customer, result);
          if (result.hasErrors()) {
              return "form/customer";
          } else {
             ...
          }
      }
  </code></pre>
  
  <p><strong>CustomValidator.java</strong></p>
  
  <pre><code>public class CustomerValidator implements Validator {
  
      @Override
      public boolean supports(Class&lt;?&gt; type) {
          return Customer.class.equals(type);
      }
  
      @Override
      public void validate(Object target, Errors errors) {
          Customer customer = (Customer) target;
         int countyid=Integer.parseInt(customer.getCountry().getCountry());
          if (countyid==0) {
               errors.rejectValue("country",  "This value is cannot be empty");
          }
      }
  }
  </code></pre>
  
  <p><strong>Customer.java</strong></p>
  
  <pre><code>   private Country country;
  </code></pre>
  
  <p><strong>Validation is working perfectly fine.</strong> But the problem is that the validation method has attached another message too. 
  <a href="https://i.stack.imgur.com/KCpmr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KCpmr.png" alt="validation view"></a>
  <br/>
  Please tell me how to correct this message. </p>