title: Bean Validation on method
body: <pre><code>public class Register {
      @NotNull private String password;
      @NotNull private String passwordRepeat;
      @AssertTrue private boolean comparePasswords() { 
         return password.equals(passwordRepeat);
      }
  
      private Set&lt;ConstraintViolation&lt;Register&gt;&gt; violations;
  
      public void doRegister(AjaxBehaviorEvent event) {
          Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
          violations = validator.validate(this);
  
          if(violations.isEmpty()) {
              // This occurs
          }
      }
  }
  </code></pre>
  
  <p>My validation will pass if both my passwords are not null, but they are different.
  Seems like the last constraint will not be taken into account, though I do not have a clue why. Does anyone have suggestion?</p>
  
  <p>And no, I am not searching for any implementations of @Matches or simular custom validators. I just would like to get this problem solved.</p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>Update 1</strong></p>
  
  <p>I have run some tests regarding this, hopefully the results will provide needed information.</p>
  
  <p><em>Bean.java</em></p>
  
  <pre><code>@Named
  @RequestScoped
  public class Bean {
      @NotNull private String example1;
      @NotNull private String example2;
      @AssertTrue private boolean examplesMatch() { return example1.equals(example2); }
  
      private Set&lt;ConstraintViolation&lt;Bean&gt;&gt; violations;
      private FacesContext context;
      private Validator validator;
  
      @PostConstruct
      public void init() {
          context = FacesContext.getCurrentInstance();
          validator = Validation.buildDefaultValidatorFactory().getValidator();
  
          example1 = "abc";
          example2 = "def";
          runValidation(false, 1);
  
          example1 = "abc";
          example2 = "abc";
          runValidation(true, 2);
  
          example1 = "abc";
          example2 = null;
          runValidation(false, 3);
      }
  
      private void runValidation(boolean assertion, int testNr) {
          FacesMessage message;
          violations = validator.validate(this);
          if(violations.isEmpty() == assertion) {
              message = new FacesMessage("Passed test nr. " + testNr);
          }
          else {
              message = new FacesMessage("Failed test nr. " + testNr);
          }
          context.addMessage(FacesMessage.FACES_MESSAGES, message);
      }
  </code></pre>
  
  <p><em>index.xhtml</em></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:h="http://java.sun.com/jsf/html"&gt;
      &lt;head&gt;
          &lt;title&gt;TODO supply a title&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          #{bean}
          &lt;h:messages /&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><em>Result</em></p>
  
  <pre><code>beanvalidation.Bean@31c5c3da
  
      Failed test nr. 1
      Passed test nr. 2
      Passed test nr. 3 
  </code></pre>