(Quick Reference)



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


class Book {

String title Author author

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


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()

At runtime the static constraints property is a Map such that the keys in the Map are property names and the values associated with the keys are instances of ConstrainedProperty:

def constraintsMap = Book.constraints
for(entry in constraintsMap) {
    // propertyName is a String
    def propertyName = entry.key

// constrainedProperty is a ConstrainedProperty def constrainedProperty = entry.value

// … }