Using Grails formatDate in input tag’s value attribute

In my GSP, I had an input tag where I wanted to set the value attribute to a date, and then use jQuery’s Datepicker widget to render the date field.  The default date format for the Datepicker widget is ‘mm/dd/yy’.  That was fine, but the widget would not show the date selected because the value of the field was not set correctly on the input field.  This is what I was doing:

<input type="text" id="endDate" value="${objInstance?.endDate}" />

In this case, endDate is a java.util.Date.  When this value is displayed, it uses toString() on java.util.Date, which is not the ‘mm/dd/yyyy’ format than jQuery wants.

That meant I needed to format the value.  Grails provides the formatDate tag as a wrapper to SimpleDateFormat class to format dates.  However, I could not use the tag lib syntax in my value attribute.  I could do this though:

<input type="text" id="endDate" value="${formatDate(format:'MM/dd/yyyy',date:objInstance?.endDate)}" />

After that my date field worked great!  When I would click on the field, the jQuery Datepicker would render and the date would be selected in the widget.

Advertisements

About stevewall123

I am a Lead Software Engineer in Minneapolis working for Thomson Reuters. I am currently working on projects using Java, JavaScript, Spring, Drools, Hazelcast, Liquibase and Tomcat. Previously, I used C#, GWT, Grails, Groovy, JMS and JBoss Drools Guvnor. In the past I have worked on projects using J2EE, Swing, Webwork, Hibernate, Spring, Spring-WS, JMS, JUnit and Ant.
This entry was posted in Grails, jQuery and tagged , . Bookmark the permalink.

One Response to Using Grails formatDate in input tag’s value attribute

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s