doumangzhen7204 2016-01-20 01:33
浏览 47
已采纳

CakePHP 3 - 使用LocalStringFormat解析日期以更正SQL格式并更正验证

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.

  • 写回答

1条回答 默认 最新

  • dtsc14683 2016-01-20 02:05
    关注

    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 > \Cake\Validation\Validation::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 App\Validation;
    
    use Cake\Core\Configure;
    use Cake\Validation\Validation;
    
    class AppValidation
    {
        public static function date($check) {
            return Validation::date($check, Configure::read('Locale.validation.dateFormat'));
        }
    }
    
    $validator->provider('appValidation', 'App\Validation\AppValidation');
    
    $validator->add('datefield', 'valid', [
        'rule' => 'date',
        'provider' => 'appValidation'
    ])
    

    * untested example code for illustration purposes

    See also Cookbook > Validation > Custom Validation Rules

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部