Search code examples
jquerygrailsdatetimepicker

g:formatDate as value on g:textField


I have this .gsp file which uses the JQuery.datetimepicker() to a particular <g:textField >. When the <g:form> of the <g:textField> is submitted, I have to convert from java.lang.String to java.util.Date following the steps provided by RoseIndia to match with the property of the pertaining domain of the concerned controller.

The problem is having to display the java.util.Date property of the domain back to java.lang.String to be displayed in the <g:textField> following to format of JQuery.datetimepicker(). I have tried to use <g:formateDate> which worked but I have to display the result back to the <g:textField> for me to use the JQuery.datetimepicker() to that particular field.

The Javascript:

$("#datetime").datetimepicker({
  timeFormat: 'hh:mm:ss',
  dateFormat: 'dd-mm-yy'
});

In the body:

<!-- Error here which return a GrailsTagException -->
<% println ${formatDate(format:"dd-MM-yyyy HH:mm:ss", date:"model?.createdDate")} %>
<g:formatDate format="dd-MM-yyyy HH:mm:ss" date="${model?.createdDate}"/>
<g:textField name="dateReported" id="datetime" value="${model?.createdDate}"/>

Solution

  • I may be misreading your issue but can't you do:

    <g:textField name="dateReported" value="${formatDate(format:"dd-MM-yyyy HH:mm:ss", date:model?.createdDate)}"/>.

    Note the removal of the quotes around "model?.createdDate" as it will eval this as a string not a reference to the date itself. Also you don't need the id the taglib will create this for you with the same name as the "name" attribute.