we have declared the following lines in the initialize function of our AppController to have globally the same format for displaying dates:
// default time formats on load
Time::$defaultLocale = 'de-DE';
Time::setToStringFormat('dd.MM.YYYY');
This worked fine. The date is displayed correcly in the view. But we get an validation error if we want to save the entity with the the date field (The provided value is invalid). The validator is configured like so:
$validator
->add('datefield', 'valid', ['rule' => 'date'])
->allowEmpty('datefield');
Here a debug of the entity with the date field:
Before patchEntity:
'datefield' => '08.07.2014'
After patchEntity:
'datefield' => object(Cake\I18n\Time) {
'time' => '2014-07-08T00:00:00+0000',
'timezone' => 'UTC',
'fixedNowTime' => false
},
....
'[errors]' => [
'datefield' => [
'valid' => 'The provided value is invalid'
]
],
Is there a way to always parse the Date globally in the correct format for saving the entity and validation.