12.1 Declaring Constraints

Within a domain class constraints are defined with the constraints property that is assigned a code block:

  1. class User {
  2. String login
  3. String password
  4. String email
  5. Integer age
  6. static constraints = {
  7. ...
  8. }
  9. }

You then use method calls that match the property name for which the constraint applies in combination with named parameters to specify constraints:

  1. class User {
  2. ...
  3. static constraints = {
  4. login size: 5..15, blank: false, unique: true
  5. password size: 5..15, blank: false
  6. email email: true, blank: false
  7. age min: 18
  8. }
  9. }

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 also applied other constraints to the password, email and age properties.

By default, all domain class properties are not nullable (i.e. they have an implicit nullable: false constraint).

A complete reference for the available constraints can be found in the Quick Reference section under the Constraints heading.

Note that constraints are only evaluated once which may be relevant for a constraint that relies on a value like an instance of java.util.Date.

  1. class User {
  2. ...
  3. static constraints = {
  4. // this Date object is created when the constraints are evaluated, not
  5. // each time an instance of the User class is validated.
  6. birthDate max: new Date()
  7. }
  8. }

A word of warning - referencing domain class properties from constraints

It’s very easy to attempt to reference instance variables from the static constraints block, but this isn’t legal in Groovy (or Java). If you do so, you will get a MissingPropertyException for your trouble. For example, you may try

  1. class Response {
  2. Survey survey
  3. Answer answer
  4. static constraints = {
  5. survey blank: false
  6. answer blank: false, inList: survey.answers
  7. }
  8. }

See how the inList constraint references the instance property survey? That won’t work. Instead, use a custom validator:

  1. class Response {
  2. ...
  3. static constraints = {
  4. survey blank: false
  5. answer blank: false, validator: { val, obj -> val in obj.survey.answers }
  6. }
  7. }

In this example, the obj argument to the custom validator is the domain instance that is being validated, so we can access its survey property and return a boolean to indicate whether the new value for the answer property, val, is valid.