title: Spring boot custom field validation
body: <p>I am trying to make the following validations:</p>
  
  <ul>
  <li>Checking in an email exists in the database.</li>
  <li>Checking in an username exists in the database.</li>
  <li>Checking if the password confirmation is entered correctly.</li>
  </ul>
  
  <p>I am using a class for validation. Example:</p>
  
  <pre><code>public class RegisterRequest {
  
      @NotNull(message = "Please enter an email")
      @Email(message = "Not a valid email")
      @JsonProperty("email")
      public String email;
  
      @NotNull(message = "Please enter a name")
      @JsonProperty("name")
      public String name;
  
      @NotNull(message = "Please enter a password")
      @JsonProperty("password")
      public String password;
  
      @NotNull
      @JsonProperty("password_confirmation")
      public String passwordConfirmation;
  }
  </code></pre>
  
  <p>This is all working fine until this far.<br>
  But now I want to build in the password validation(Check if 2 password's match). I will only use this one time, so I think it is unnecessary to create a whole Annotation for this.<br>
  How can I do this without the use of an Annotation.</p>
  
  <p>The solution I found that comes closes to what I want is to use <code>@AssertTrue</code> on a method that validates the passwords:</p>
  
  <pre><code>@AssertTrue("Passwords don't match")
  public boolean checkPasswords() {
      return this.password.equals(this.passwordConfirmation);
  }
  </code></pre>
  
  <p>This works but it is not linked to a field, but it is a global error.<br>
  So I can't find out to what field the <code>Passwords don't match</code> error belongs.
  Is there a way to link the <code>checkPasswords</code> validation to a field?</p>