Forcing No Selection on a Grails datePicker

I just came across this problem using the datePicker tag in a gsp – it appeared that there was no way to stop the tag showing today’s date when no value was set on the bean.

My tag looked like this:

<g:datePicker name="myDate"
    value="${myBean.aDate}"
    precision="day"
    years="${[2009, 2010, 2011]}"
    noSelection="${['':'--']}"/>

Whenever myBean.aDate was null the tag still displayed today’s date and no matter how I read the datePicker docs there seemed to be no solution, that is until I happened upon a mailing list post which showed a “default” attribute:

<g:datePicker name="myDate"
    value="${myBean.aDate}"
    precision="day"
    years="${[2009, 2010, 2011]}"
    noSelection="${['':'--']}"
    default="none"/>

The addition of “default=none” stops the tag showing today’s date and instead reverts to the noSelection value as I had originally expected. I have no idea what other values this “default” attribute can take, or where the code that reads it is in the Grails source (a search for datePicker in the source yielded nothing useful) so I can’t say anything more about it!

As soon as the Grails JIRA is back up (under maintenance at the moment, apparently) I’ll raise a bug on the docs.

Advertisement

4 thoughts on “Forcing No Selection on a Grails datePicker

    • Hi David, are you using the latest version of Grails (1.3.4)? If so there’s a bug which I discovered only last week that causes a new Date object to appear in your controller’s params when there’s no value set in the browser.

      My solution, which differs slightly from the one in the bug report, was:

      if (params.<datefield_name>_day == ”
      || params.<datefield_name>_month == ”
      || params.<datefield_name>_year == ”) {
      params.remove(‘<datefield_name>’)
      }

      Maybe that will work for you, too?

      Dave

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