SilverStripe: Please enter a valid date format
From FVue
Contents |
Problem
After having changed the default locale in SilverStripe to `nl_NL', using this command in _config.php:
i18n::set_default_locale('nl_NL');
submitting Date fields will yield an error:
Please enter a valid date format (d MMM yyyy).
Environment
- SilverStripe-2.4.5
Solution
The solution is to add a set_date_format to mysite/_config.php:
i18n::set_default_locale('nl_NL'); Translatable::set_default_locale('nl_NL'); // Use only numbers to satisfy Zend_Locale_Format::_parseDate() // Use slashes to satisfy datepicker validation; format 'dd-mm-yyyy' isn't recognized by the Javascript validation...? i18n::set_date_format('dd/mm/yyyy');
In short, this is because there seems to be a problem with Zend_Locale_Format::_parseDate() matching full month names only... and the Javascript datepicker uses Zend_Locale_Format::_parseDate() to validate dates.
Also, the Javascript validation, doesn't recognize the 'dd-mm-yyyy' format and gives an error "required" unless a slashed format (dd/mm/yyyy) is used.
Rationale
If you put only this in mysite/_config.php:
i18n::set_default_locale('nl_NL');
the call stack will become:
DateField::__construct()
i18n::get_date_format()
Zend_Locale_Format::getDateFormat('nl_NL)
Zend_Locate_Data::getContent('nl_NL', 'date')
Zend_Locate_Data::getContent() defaults to use dateformat "gregorian/medium" and so will retrieve
/ldml/dates/calendars/calendar[@type='gregorian']/dateFormats/dateFormatLength[@type='medium']/dateFormat/pattern
from the file:
sapphire/thirdparty/Zend/Locale/Data/nl.xml
which is set to 'd MMM yyyy'.
"MMM" denotes charaters, i.e. "Oct", "Nov", "Dec".
Zend_Locale_Format::_parseDate(), however, does a non-matching search on full month names ("October", "November", "December") and a subsequent split on numbers (\d+):
preg_match_all('/\d+/u', $number, $splitted);
So e.g. "15 Dec 2012" gets splitted to:
[0] => 15 [1] => 2012
"2012" will be read as the "MMM" month and Zend will die because month > 12.