title: Play: Custom Ebean constraints
body: <p>I just started with Play and am trying to get some custom constraints on my model. </p>
  
  <p>I have found that this works</p>
  
  <pre><code>@Constraints.Required
  @Constraints.MaxLength(15)
  @Constraints.MinLength(4)
  @Constraints.Pattern(value = "^\\w\\s$")
  public String username;
  </code></pre>
  
  <p>But now I want to add a custom constraint with an own implementation (for checking for uniqueness) So I tried this (it was auto-completed by my IDE so it does exist)</p>
  
  <pre><code>@Constraints.ValidateWith(Account.UniqueValidator.class)
  public String username;
  </code></pre>
  
  <p>I found somewhere (have gone through a lot of links so can't find it anymore, that the class should extend Constraints.Validator</p>
  
  <p>So that is what I did.</p>
  
  <pre><code> private class UniqueValidator extends Constraints.Validator&lt;String&gt;
  {
  
      @Override
      public boolean isValid(String s) {
          return Account.find.where().eq("username", s).findRowCount() == 0;
      }
  
      @Override
      public F.Tuple&lt;String, Object[]&gt; getErrorMessageKey() {
          return null;
      }
  }
  </code></pre>
  
  <p>However I don't know what to return in the getErrorMessageKey value, it is a required method to implement. I looked in the play source and I found "something" what looks like this. Namely the <a href="https://github.com/playframework/playframework/blob/834310188560715aa43b74abf86032c81fe5d4fe/framework/test/integrationtest/app/validator/NotEmpty.java" rel="nofollow">NotEmpty</a> validator.</p>
  
  <p>However when you look there you see it returns an Tuple, not an F.Tuple. When I change that in my code it complains that it is not an F.Tuple.</p>
  
  <p>Tried to return </p>
  
  <pre><code>return Tuple("error.invalid", new Object[] {});
  </code></pre>
  
  <p>As done in the NotEmpty validator link above. But than it says that it needs "new" before Tuple, but that gives me an class with <a href="https://gist.github.com/Matthijn/6555710" rel="nofollow">a bunch</a> of required methods to implement. So I guess that is not really the way to go.</p>
  
  <hr>
  
  <p>When I look at the documentation of play <a href="http://www.playframework.com/documentation/1.1.1/validation" rel="nofollow">here</a> I need to use the @CheckWith annotation, but when I choose that it says that it cannot find the @CheckWith annotation.</p>
  
  <p>I am lost. :-) </p>