Since the idea to set constraints in the model came from grails I have been looking at a lot of their constraints and I wanted to pick your brains on a few things. In this release you are going to have the ability to define constraints like so
component persistent="true" table="Users" extends="BaseEntity" {
property name="id" column="user_id";
property name="firstName";
property name="lastName";
property name="email";
property name="age";
property name="username";
property name="password";
property name="passwordConfirm" persistent="false";
property name="type";
property name="roles" fieldtype="many-to-many" cfc="Role" singularname="role" fkcolumn="user_id" inversejoincolumn="role_id" linktable="users_roles";
// model constraints
this.constraints = [
{property="firstName",blank=false},
{property="lastName",blank=false},
{property="email",blank=false,email=true,unique=true,context="create,foo,bar"},
{property="age",blank=false,size="18..30"},
{property="passwordConfirm",blank=false,validator="passwordsMatch"},
{property="type",blank=false,inList="employee,manager"}
];
public User function init(){
return this;
}
// custom validators
public boolean function passwordsMatch(){
if( getPassword() == getPasswordConfirm() ){
return true;
}
return false;
}
}
1.) With a restraint like blank=false you are telling hyrule that you do not want to allow a blank value.
a) should this be a string only or check for a blank on any value (empty collection/empty array)
b) would a user ever say blank=true.. ie should we ever test for that?
a.) should min and max only test numbers or all simple values (strings/dates/numbers)
b.) if so grails has a minSize/maxSize we could do the same to test collection sizes
a) would a user ever say creditcard=false... why would you ever test to see if something is not a credit card.
4.) I have some implementation questions on unique but I will save that for another thread.