Search code examples
grailsgroovy-console

When does Grails constraints work?


It looks like a basic issue, that I am trying to test Grails constraints in groovy console tool.

Does it work only when I do any database CRUD operations?

Sample code is like this

class AAA{
    String a
    String b
    static constraints = {
         a size:5..8
         b nullable:false
    }
}

def x = new AAA(a:'sss',b:null)
println x.a
println x.b

Output is

sss
null

Why are they not working?


Solution

  • Yes because the object.validate() method is only going to validate these constraints before saving the object.According to the official documentation of grails .

    Constraints provide Grails with a declarative DSL for defining validation rules, schema generation and CRUD generation meta data. .See below

    http://grails.org/doc/2.3.x/ref/Constraints/Usage.html