On 7/01/2014 8:41 PM, Adam Witney wrote:


On 6. 1. 2014 21:41, Bill Moseley wrote:

On Mon, Jan 6, 2014 at 7:18 AM, Adam Witney <awit...@sgul.ac.uk
<mailto:awit...@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.
Yep. Your form response param is going to be text and not a datetime object anymore. At any rate here is your case if I am reading correctly.

1. Model returns an inflated Datetime object. This is parsed from a string based on the Driver backend you have. Which is what inflate column does. 2. You don't want to have the default "stringify" displayed. So either change the stashed value or represent differently in view. 3. You *Are* going to get back a string in the raw params. So what you need to do is parse that format into a DateTime object again when submitting back to model or wherever else you want to use it as an object.

You can either do this in your controller directly or look at the various Form handling modules on CPAN where you can set up the Inflation/Deflation to your needs. All of these give you another object to deal with but raw input params are just going to be strings without further handling.

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

_______________________________________________
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/


---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com


_______________________________________________
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/

Reply via email to