Within a domain class a
constraints are defined with the constraints property that is assigned a code block:
class User {
String login
String password
String email
Integer age static constraints = {
…
}
}
You then use method calls that match the property name for which the constraint applies in combination with named parameters to specify constraints:
class User {
... static constraints = {
login(size:5..15, blank:false, unique:true)
password(size:5..15, blank:false)
email(email:true, blank:false)
age(min:18, nullable:false)
}
}
In this example we've declared that the
login
property must be between 5 and 15 characters long, it cannot be blank and must be unique. We've all applied other constraints to the
password
,
email
and
age
properties.
A complete reference for the available constraints can be found on the reference guide