Constraints Usage

Constraints provide Grails with a declarative DSL for defining validation rules, schema generation and CRUD generation meta data. An example of a set of constraints applied to a domain class are as follows:

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

Refer to the user guide topic on Constraints for more information.

Global Constraints

You can apply constraints globally inside grails-app/conf/Config.groovy as follows:

grails.gorm.default.constraints = {
   '*'(nullable:true, size:1..20)
}

The wildcard signifies that the constraints apply to all properties. You can also define shared constraints:

grails.gorm.default.constraints = {
   myShared(nullable:true, size:1..20)
}

That can be reused in your class:

class User {
    ...

static constraints = { login(shared:"myShared") } }