[Catalyst] Changing format of date field

Adam Witney awitney at sgul.ac.uk
Tue Jan 7 09:41:22 GMT 2014



On 6. 1. 2014 21:41, Bill Moseley wrote:
>
> On Mon, Jan 6, 2014 at 7:18 AM, Adam Witney <awitney at sgul.ac.uk
> <mailto:awitney at sgul.ac.uk>> wrote:
>
>     Hi,
>
>     I have a date field in a DBIx::Class Result class using
>     InflateColumn::DateTime. When I pass this through a Catalyst/TT
>     application, the date is presented in YYYY-MM-DD format.
>
>     I can modify this to DD/MM/YYYY in my TT template with
>     object.dmy('/'), but I would prefer to change the default DateTime
>     stringification, however I am not quite sure where or how to do this?
>
>     Any ideas greatly appreciated.
>
>
> Seem like formatting in the template is the right place -- may want to
> have different formats in different places in your app.   Then you might
> also think about how best to localize.
>
> One idea is to localize a set of names for a set of formats:
>
> [% dt_fmt = c.localize( '_DT_DATE_ONLY' );
> foo.some_dt_object.strftime(  dt_fmt ) | html %]
>
>
> I have also used a function that does the above, but also clones and
> sets the time zone and locale based on the user's preferences:
>
> [% user_time( foo.event_start, '_DT_TIMESTAMP_WITH_ZONE' ) | html %]

Hi Bill,

Thanks for your email. I agree that the template seems like a good place 
for this, but to give a little extra detail.

I have a form that is used for both object create and editing. When I 
edit an object I have this in my controller:

$c->stash(formdata => $object);

and in the template:

<input name="datefield" type="text" value="[% 
formdata.datefield.dmy('/') %]" />

But when creating a new object, if the form validation fails I pass the 
form data back to the form like so:

$c->stash( formdata => $c->request->params );

But now the datefield is a text string and formdata.datefield.dmy('/') 
prints blank.

I was thinking that if I could define my DateTime stringification to my 
required format centrally then I could just use this in the template

<input name="datefield" type="text" value="[% formdata.datefield %]" />

and both scenarios would be happy.

Is the only way to do this to put more code in the template?

Thanks again for your help

Adam



More information about the Catalyst mailing list