@Entity
@Table(uniqueConstraints = @UniqueConstraint(columnNames = { "field1", "field2"}))
public class A extends Model{
public String field1;
public String field2;
}
I would like to define my own check constraint by extending play.data.validation.Check so I can display a custom validation message.
However it seems that Check only applies to one field.
Is there another way to do this validation check on multiple fields?
UPDATE – Solution
@Entity
@Table(uniqueConstraints = @UniqueConstraint(columnNames = { "field1", "field2"}))
public class A extends Model{
@CheckWith(UniqueFieldsCheck.class)
public String field1;
public String field2;
static class UniqueFieldsCheck extends Check {
public boolean isSatisfied(Object obj, Object o) {
boolean satisfied = true;
A a = (A)obj;
A aA = A.find("byField1AndField2", a.field1, a.field2).first();
if (aA != null) {
setMessage("This fields should be unique.");
satisfied = false;
}
return satisfied;
}
}
}
Well no, Play only let’s you validate on each field individually. However you can make you own composite field, that’s contained in a separate class and add it to you main object, something like:
and then apply a custom validator to it (more on those here)