Format dates in show view

40 views
Skip to first unread message

Chris Malan

unread,
Feb 11, 2016, 5:44:52 PM2/11/16
to Grails Dev Discuss
With the fields plugin things are a bit different.  I have /grails-app/views/_fields/date/_displayWrapper.gsp.  This file contains only this:
<%@ page defaultCodec="html" %>

<g:formatDate format="${constraints.format}" date="${value}" />

<%-- This works for the list view, but not the show view --%>

As the comment says, it works perfectly in the list/index view, but not the show view.  The format is supplied in the domain class constraints.  Getting the date pickers to render as I want them works perfectly with _widget.gsp.  I found what it needed on the web.  This is what it contains:
<%@ page defaultCodec="html" %>

<g:if test="${constraints.nullable == true}">
<g:datePicker name="${property}" value="${value}" precision="${constraints.attributes.precision}" years="${constraints.attributes.years}" default="none" noSelection="['': '']" />
</g:if>
<g:else>
<g:datePicker name="${property}" value="${value}" precision="${constraints.attributes.precision}" years="${constraints.attributes.years}"/>
</g:else>

Without the _displayWrapper.gsp the domain class constraint format doesn't work at all.  Any idea how to get the dates to show in the right format in the show view? 

Thanks.

Chris

Chris Malan

unread,
Mar 14, 2016, 4:56:47 PM3/14/16
to Grails Dev Discuss
OK, here it is.  It's a three step process.

First, in the domain class set the format in the constraints block like so:
toTheMinute nullable: false, format: 'dd MMM yyyy HH:mm'

Then create a file, _wrapper.gsp in grails-home/views/_fields/showDate  This showDate can be anything that takes your fancy.  This _wrapper.gsp looks like this:
 <li class="fieldcontain" style="list-style-type: none;">
<span class="property-label">${label}</span>
<div class="property-value">

<g:formatDate format="${constraints.format}" date="${value}" />
</div>
</li>

The last part is in the relevant show.gsp.  Here's what the relevant bit in mine looks like:
<ol class="property-list dates">
<f:with bean="dates">
<f:field property="toTheMinute" wrapper="showDate" />
<f:field property="toTheDay" wrapper="showDate" />
<f:field property="nullableDate" wrapper="showDate" />
<f:field property="dateCreated" wrapper="showDate" />
<f:field property="lastUpdated" wrapper="showDate" />
</f:with>
</ol>

This works.  Does anybody know of a way that doesn't need one specifying all fields in the domain class?

Chris Malan 
Reply all
Reply to author
Forward
0 new messages