doumangzhen7204 2016-01-20 09: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 10: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

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

报告相同问题?

悬赏问题

  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败