(Quick Reference)

20.3 Adding Constraints

Version: 3.2.11

20.3 Adding Constraints

You can still use GORM validation even if you use a Java domain model. Grails lets you define constraints through separate scripts in the src/main/groovy/ directory. The script must be in a directory that matches the package of the corresponding domain class and its name must have a Constraints suffix. For example, if you had a domain class org.example.Book, then you would create the script src/main/groovy/org/example/BookConstraints.groovy\.

Add a standard GORM constraints block to the script:

constraints = {
    title blank: false
    author blank: false
}

Once this is in place you can validate instances of your domain class!