久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

CakePHP 3 - 使用 LocalStringFormat 解析日期以更正 SQ

CakePHP 3 - Parse Date with LocalStringFormat to correct SQL format and correct validation(CakePHP 3 - 使用 LocalStringFormat 解析日期以更正 SQL 格式和正確驗證)
本文介紹了CakePHP 3 - 使用 LocalStringFormat 解析日期以更正 SQL 格式和正確驗證的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我們在 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 slash
  • mdy 12-27-2006 or 12-27-06 separators can be a space, period, dash, forward slash
  • ymd 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)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Cannot use #39;Object as class name as it is reserved Cake 2.2.x(不能使用 Object 作為類名,因為它是保留的 Cake 2.2.x)
Session is lost after an OAuth redirect(OAuth 重定向后會話丟失)
Pagination Sort in Cakephp 3.x(Cakephp 3.x 中的分頁排序)
CakePHP Shared core for multiple apps(CakePHP 多個應(yīng)用程序的共享核心)
Login [ Auth-gt;identify() ] always false on CakePHP 3(在 CakePHP 3 上登錄 [ Auth-identify() ] 始終為 false)
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 87 bytes)(致命錯誤:允許的內(nèi)存大小為 134217728 字節(jié)已用盡(嘗試分配 87 字節(jié)))
主站蜘蛛池模板: 成人av色| 国产精华一区 | 天天舔天天 | 欧美一区免费在线观看 | 91精品国产欧美一区二区 | 欧美在线一区二区三区四区 | 国产精品久久久亚洲 | 成人午夜免费网站 | 国产综合精品一区二区三区 | 中国三级黄色录像 | 91一区二区 | 日韩精品视频在线观看一区二区三区 | 国产精品视频免费观看 | 欧美精品一区三区 | av免费网站在线观看 | 久久综合成人精品亚洲另类欧美 | 久久精品中文 | 国产精品一区久久久 | 一级黄色生活视频 | 国产一区二区三区在线 | 久久精品无码一区二区三区 | 男女羞羞视频在线看 | 中文在线一区二区 | 午夜精品一区 | 精品国产乱码久久久久久牛牛 | 伊人免费观看视频 | www.日本国产 | 男人天堂网av | 久久久久一区 | 欧美成人精品一区二区男人看 | 精品国产免费一区二区三区演员表 | 久久综合九色综合欧美狠狠 | 国产成人一区二区三区电影 | 粉嫩av| 在线日韩欧美 | 国产精品夜夜春夜夜爽久久电影 | 色播99| 日韩中文一区二区 | 欧美精品一区二区免费 | 欧美国产精品 | 国产亚洲一区二区在线观看 |