(Quick Reference)

constraints

Purpose

Allows the definition of declarative validation constraints. See Validation in the user guide.

Examples

class Book {

String title Author author

static constraints = { title blank: false, size: 5..150 author nullable: true } }

Description

Constraints are defined using the declarative constraints DSL as described in the Validation section of the user guide. Once evaluated, validation can be applied through the use of the validate method:

def b = new Book()
assert !b.validate()

A list of ConstrainedProperty instances applied against the domain class by the constraints property is available at runtime:

for (constraint in b.constraints) {
    println it.name
    println it.maxSize
}