title: Spring Validator for duplicate record in database
body: <p>In my page jsp, i have a form where i can add a user to my database, and i use a validator to show errors when fields are empty, but what i want to do is, when i insert a duplicate entry of the primary key of my table, the validator show me a message that this login for example is already taken instead having an error by Apache!</p>
  
  <p>this  is my User POJO : </p>
  
  <pre><code>package gestion.delegation.domaine;
  
  import java.io.Serializable;
  
  public class User implements Serializable{
      /**
       * 
       */
      private static final long serialVersionUID = 1L;
      int id;
      String nom;
      String prenom;
      String login;
      String password;
      String role;
      boolean enable;
      public int getId() {
          return id;
      }
      public void setId(int id) {
          this.id = id;
      }
      public String getNom() {
          return nom;
      }
      public void setNom(String nom) {
          this.nom = nom;
      }
      public String getPrenom() {
          return prenom;
      }
      public void setPrenom(String prenom) {
          this.prenom = prenom;
      }
      public String getLogin() {
          return login;
      }
      public void setLogin(String login) {
          this.login = login;
      }
      public String getPassword() {
          return password;
      }
      public void setPassword(String password) {
          this.password = password;
      }
      public boolean getEnable() {
          return this.enable;
      }
      public void setEnable(boolean enable) {
          this.enable = enable;
      }
  
      public User(int id, String nom, String prenom, String login,
              String password, String role, boolean enable) {
          super();
          this.id = id;
          this.nom = nom;
          this.prenom = prenom;
          this.login = login;
          this.password = password;
          this.role = role;
          this.enable = enable;
      }
      public String getRole() {
          return role;
      }
      public void setRole(String role) {
          this.role = role;
      }
      public User() {
          super();
      }
  
  }
  </code></pre>
  
  <p>and this is my validator : </p>
  
  <pre><code>package gestion.delegation.validator;
  
  import gestion.delegation.domaine.User;
  
  import org.springframework.validation.Errors;
  import org.springframework.validation.ValidationUtils;
  import org.springframework.validation.Validator;
  
  public class AddUserValidator implements Validator{
  
      @Override
      public boolean supports(Class&lt;?&gt; clazz) {
  
          return User.class.isAssignableFrom(clazz);
      }
  
      @Override
      public void validate(Object obj, Errors err) {
          ValidationUtils.rejectIfEmptyOrWhitespace(err, "nom", "name.required","Choisissez un nom");
          ValidationUtils.rejectIfEmptyOrWhitespace(err, "prenom", "prenom.required", "Choisissez un prenom");
          ValidationUtils.rejectIfEmptyOrWhitespace(err, "login", "login.required", "Choisissez un login");
          ValidationUtils.rejectIfEmptyOrWhitespace(err, "password", "password.required", "Choisissez un password");
          ValidationUtils.rejectIfEmpty(err, "role", "role.required", "Choisissez un role");
  
      }
  
  }
  </code></pre>
  
  <p>the form : </p>
  
  <pre><code>&lt;c:if test="${not empty msg_success}"&gt;
                      &lt;div class="success"&gt;Vous avez ajouter un utilisateur avec
                          succès !&lt;/div&gt;
  &lt;/c:if&gt;
                  &lt;form:form name="ajf"
                      action="${pageContext.request.contextPath}/ajouter_user"
                      method="post" commandName="user"&gt;
  
                      &lt;table id="tabmenu"&gt;
  
  
                          &lt;tr&gt;
                              &lt;td id="idtab"&gt;Nom :&lt;/td&gt;
                              &lt;td&gt;&lt;form:input type="text" path="nom"
                                      class="round default-width-input" name="name_" /&gt;&lt;/td&gt;
                              &lt;td&gt;&lt;form:errors path="nom" Class="errorbox" /&gt;&lt;/td&gt;
                          &lt;/tr&gt;
                          &lt;tr&gt;
                              &lt;td id="idtab"&gt;Prénom :&lt;/td&gt;
                              &lt;td&gt;&lt;form:input type="text" path="prenom" name="prenom_"
                                      class="round default-width-input" /&gt;&lt;/td&gt;
                              &lt;td&gt;&lt;form:errors path="prenom" cssClass="errorbox" /&gt;
                          &lt;/tr&gt;
                          &lt;tr&gt;
                              &lt;td id="idtab"&gt;Login :&lt;/td&gt;
                              &lt;td&gt;&lt;form:input type="text" path="login" name="login_"
                                      cssClass="round default-width-input" /&gt;&lt;/td&gt;
                              &lt;td&gt;&lt;form:errors path="login" cssClass="errorbox" /&gt;&lt;/td&gt;
                          &lt;/tr&gt;
  
                          &lt;tr&gt;
                              &lt;td id="idtab"&gt;Password :&lt;/td&gt;
                              &lt;td&gt;&lt;form:input type="password" path="password" name="pass_"
                                      class="round default-width-input" /&gt;&lt;/td&gt;
                              &lt;td&gt;&lt;form:errors path="password" cssClass="errorbox" /&gt;&lt;/td&gt;
  
                          &lt;/tr&gt;
  
                          &lt;tr&gt;
                              &lt;td id="idtab"&gt;Séléctionner un rôle :&lt;/td&gt;
                              &lt;td&gt;&lt;form:select path="role"&gt;
                                      &lt;form:option value="" label="" /&gt;
                                      &lt;form:option value="ROLE_ADMIN"&gt;Administrateur&lt;/form:option&gt;
                                      &lt;form:option value="ROLE_USER"&gt;Simple utilisateur&lt;/form:option&gt;
                                  &lt;/form:select&gt;&lt;/td&gt;
                              &lt;td&gt;&lt;form:errors path="role" cssClass="errorbox" /&gt;&lt;/td&gt;
                          &lt;/tr&gt;
                          &lt;tr&gt;
                              &lt;td id="idtab"&gt;Activé :&lt;/td&gt;
                              &lt;td&gt;&lt;form:input type="checkbox" value="true" path="enable" /&gt;
                                  Oui&lt;/td&gt;
                          &lt;/tr&gt;
                          &lt;tr&gt;&lt;/tr&gt;
                          &lt;tr&gt;&lt;/tr&gt;
                          &lt;tr&gt;
                              &lt;td&gt;&lt;input
                                  class="button round blue image-right ic-right-arrow"
                                  type="submit" value="Créer" /&gt;&lt;/td&gt;
                              &lt;td&gt;&lt;input
                                  class="button round blue image-right ic-right-arrow"
                                  type="reset" value="Initialiser" /&gt;&lt;/td&gt;
                          &lt;/tr&gt;
  
                      &lt;/table&gt;
                  &lt;/form:form&gt;
  </code></pre>
  
  <p>Any Idea?</p>