tags:

views:

174

answers:

1

The code

${personRequest.constraints.requestStatus.inList}

in my gsp file throws a NullPointerException, but "new PersonRequest().constraints..." works. Why? Isn't constraints static?

+3  A: 

Copied from here (grails-user mailing list)

but why accesing to static fields doesnt work?

Because the "constraints" static field is a closure property. It has no sub-properties relating to the constraints.

Grails evaluates this closure using a constraint builder which monitors "calls" to methods that don't exist, i.e. the property names you use:

type( inList: [xxxxx])

...causes a method call to "type" with a Map parameter containing "inList" ==> some value.

From this info, Grails builds up a list of all the constraints
available and binds them to future instances of the domain class.

So, when you access User.constraints statically, you are getting a
Closure.

We -could- look at replacing the value of the static constraints property at runtime after evaluating the Closure it is initialized with. However this could be a bit "too much" magic. What do people think?

FYI it's like this now because until 0.4 (if I recall) constraints weren't static, and in 0.4 they can still be non-static. Only in 0.5
will we throw an exception/ignore non-static constraints.

Epaga