問題描述
我們在 AppController 的 initialize 函數(shù)中聲明了以下幾行,以具有全局相同的日期顯示格式:
//加載時的默認(rèn)時間格式時間::$defaultLocale = 'de-DE';Time::setToStringFormat('dd.MM.YYYY');
這工作得很好.日期在視圖中正確顯示.但是,如果我們想保存帶有日期字段的實體(提供的值無效),則會出現(xiàn)驗證錯誤.驗證器配置如下:
$validator->add('datefield', 'valid', ['rule' => 'date'])->allowEmpty('datefield');
這是帶有日期字段的實體的調(diào)試:
在 patchEntity 之前:
'datefield' =>'08.07.2014'
補丁實體之后:
'datefield' =>對象(蛋糕I18n時間){'時間' =>'2014-07-08T00:00:00+0000','時區(qū)' =>'世界標(biāo)準(zhǔn)時間','fixedNowTime' =>錯誤的},....'[錯誤]' =>['日期字段' =>['有效' =>'提供的值無效']],
有沒有辦法始終以正確的格式全局解析日期以保存實體和驗證.
解析(在編組過程中)和驗證沒有關(guān)系,前者會after后者發(fā)生.
檢查 date
驗證方法 API,它需要更多參數(shù),即要使用的格式,以及要使用的自定義正則表達式,而不是預(yù)定義的.
date(string|DateTime $check, string|array $format 'ymd', string|null $regex null)
日期驗證,確定傳遞的字符串是否為有效日期.鑰匙期望完整的月、日和年將驗證閏年.
年份從 1800 年到 2999 年有效.
格式:
dmy
27-12-2006 或 27-12-06 分隔符可以是空格、句點、破折號,正斜杠mdy
12-27-2006 或 12-27-06 分隔符可以是空格、句點、破折號、正斜杠ymd
2006-12-27 或 06-12-27分隔符可以是空格、句點、破折號、正斜杠- ...
[...]
API > CakeValidationValidation::date()
因此,為了正確驗證本地化的德語數(shù)據(jù),您必須指定 dmy
格式.
->add('datefield', 'valid', ['rule' => ['date', 'dmy']])
如果您想以一種可以從應(yīng)用程序中的單個點更改格式的方式全局應(yīng)用本地化驗證,那么您可以例如使用自定義驗證規(guī)則和全局可用的自定義提供程序來獲取格式來自您的應(yīng)用配置,例如
命名空間 AppValidation;使用 CakeCoreConfigure;使用 CakeValidationValidation;類 AppValidation{公共靜態(tài)函數(shù)日期($check){返回 Validation::date($check, Configure::read('Locale.validation.dateFormat'));}}
$validator->provider('appValidation', 'AppValidationAppValidation');$validator->add('datefield', 'valid', ['規(guī)則' =>'日期','提供者' =>'應(yīng)用驗證'])
* 用于說明目的的未經(jīng)測試的示例代碼
另見食譜>驗證> 自定義驗證規(guī)則
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(CakeI18nTime) {
'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.
Parsing (in the marshalling process) and validation have nothing to do with each other, the former will happen after the latter.
Check the date
validation method API, it takes further arguments, that is, the format to use, and a custom regular expression to use instead of the predefined ones.
date(string|DateTime $check, string|array $format 'ymd', string|null $regex null)
Date validation, determines if the string passed is a valid date. keys that expect full month, day and year will validate leap years.
Years are valid from 1800 to 2999.
Formats:
dmy
27-12-2006 or 27-12-06 separators can be a space, period, dash, forward slashmdy
12-27-2006 or 12-27-06 separators can be a space, period, dash, forward slashymd
2006-12-27 or 06-12-27 separators can be a space, period, dash, forward slash- ...
[...]
API > CakeValidationValidation::date()
So in order to properly validate your localized german data, you'll have to specify the dmy
format.
->add('datefield', 'valid', ['rule' => ['date', 'dmy']])
If you want to apply localized validation globally, in a way where the format can be changed from a single point in your app, then you could for example use a custom validation rule and a globally available custom provider, which fetches the format from your apps configuration, like
namespace AppValidation;
use CakeCoreConfigure;
use CakeValidationValidation;
class AppValidation
{
public static function date($check) {
return Validation::date($check, Configure::read('Locale.validation.dateFormat'));
}
}
$validator->provider('appValidation', 'AppValidationAppValidation');
$validator->add('datefield', 'valid', [
'rule' => 'date',
'provider' => 'appValidation'
])
* untested example code for illustration purposes
See also Cookbook > Validation > Custom Validation Rules
這篇關(guān)于CakePHP 3 - 使用 LocalStringFormat 解析日期以更正 SQL 格式和正確驗證的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!