UK Date Format Help Please

Hi

I’ve changed the default locale to en_GB in my app.php and although it is displaying the dates correctly it’s causing me some confusion.

The site is for camping in the uk and allows people to book online. So i have a booking controller and it works well for all the basic CRUD stuff. I can add, edit, view bookings so i know that Cake is handling the dates properly when displaying them and when communicating with the database.

The problem arises when i try to pass response data to another action within the booking controller. There’s a sequence of actions: select-dates, add-details, additional-people. I want each one of these actions to pass the data they gather on to the next action, and i decided to use setAction to do that. I’m quite new to Cakephp so this could just be a massive misconception on my part.

So in the first action of the booking process the users select their dates and submit the form. In the controller action i use patch entity on the response data and pass the result into setAction which loads the next action of the booking process. The dates are passed successfully, but only if i use a two digit year. I’d really like to understand why that is.

When i debug the variable that the second controller sends to the view i can see that any date sent with a four digit year produces the following error: “The value provided is invalid”

I’ve tried changing the validation format for the date fields in the validator for the model but it didn’t help. I’ve read a lot about how to setup date formats but i’m finding it very confusing so i would very much appreciate some advice on this.

Cheers.

Hi

I fluked making this work before.

It all resurfaced when i deployed my app recently so i think i understand now. And it’s actually very simple.

So i wanted the dates to appear in gb format.

I think the setting that made that happen was the defaultLocal in app.php. I set it to ‘en_GB’

That only worked for display dates though. Not for input fields. They were still ‘ymd’.

So for the input fields i used a custom template for the form helper. I got the code from another post on here.

<?php return [ 'dateWidget' => '{{day}}{{month}}{{year}}{{hour}}{{minute}}{{second}}{{meridian}}', ]; In 'src/View/AppView.php' load form Helper like:

$this->loadHelper(‘Form’, [
‘templates’ => ‘my_custom_templates’
]);

Thanks to chav170 for that.

So that was the display side sorted out.

Next i found the validator was returning an error saying the value of the date field was invalid.

This was the bit that took ages to figure out, and i think the main source of my woes.

All i had to do was add a format to the date field in the validator. So instead of $validator->date(‘from’)
it became $validator->date(‘from’, [‘dmy’])

I had all kinds of issues before i found this in the api docs. I wasn’t even looking for it at the time!

People were telling me to add various bits of ‘Cake\I18n\Time’ to bootstrap.php and to add userLocaleParser(‘dd/mm/yyyy’) after Type::build(‘date’) and many other things besides. None of it did what i expected. In fact most of it had no noticeable impact that i can remember.

I had dates becoming null on patch entity; apparently valid dates being rejected; dates i wouldn’t expect to work being accepted. I can’t even remember all the issues!

Long story short: this issue turned out to be a whole lot simpler than the internet led me to believe.

People are always telling me you can’t believe everything you read on the internet; I’d have to say i agree. But Ryan Giggs is definitely made out of cheese. I don’t think anyone can argue that.