Why Won’t My Data Bind!

I love having the ability to create property getter and setter methods in Groovy and it really simplifies all those bean accessors in Grails domain classes. At least, it does until you define one incorrectly and spend an embarrassingly long time trying to work out why data in your Grails controller isn’t being bound…

The problem? I’d defined a setter in my domain class without types:

Date myDate
def setMyDate(value) {
    ...
}

It looked so normal in my code that I didn’t think anything of it, but a little debug showed me it wasn’t being called. So I changed it to

Date myDate
void setMyDate(Date value) {
    ...
}

and it worked!

Thinking about it I suppose it’s reasonable that it wouldn’t work without explicit types because how can the property be matched to the method without knowing the type. Of course, it’s all in the Groovy bean docs, in fact the rules regarding properties and rather complex.

So, why won’t my data bind? Because I hadn’t RTFM.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s