Friday 25 April 2008

Cascading validation

This has confused me for a while. If we have these classes:
class Lolrus {
String name
Bukkit bukkit
}

class Bukkit {
String contents
static constraints = {
contents(validator: {
return 'o.noes'
})
}
}

You will find this test fails because the validate() call does not cascade to the aggregated object:
void testErrorsOnAssociations() {
def lolrus = new Lolrus(name: 'jerome')
lolrus.bukkit = new Bukkit(contents: 'fish')
assert !lolrus.validate()
}

The trick is validate will only cascade in the same circumstances that a save would cascade, so if we add static belongsTo = [lolrus: Lolrus] to the Bukkit class the validation cascades properly and the test works.

No comments: