【问题标题】:Date validation in CakeCake 中的日期验证
【发布时间】:2011-03-18 14:58:19
【问题描述】:

如何在我的 Cake 应用程序中使用内置日期格式?也许我犯了一些简单的错误。我现在专注于模型代码,我想这就是我搞砸了。

在页面上看起来它正在工作,显示三个选择的小部件,按月、日、年的顺序排列。但是,当我提交表单时,我会收到“必须是有效日期”消息。

这是我的视图代码:

echo $this->Form->create('Subscription');
echo $this->Form->input('starts',array('type'=>'date','dateFormat'=>'MDY'));
echo $this->Form->end('Submit', true);

在我的模型中,验证看起来像这样:

'starts' => array(
    'date' => array(
    'rule' => array('date', array('MDY')),
    'message' => 'Must be a valid date',
    ),
    'notempty' => array(
        'rule' => array('notempty'),
        'message' => 'Start date is required',
    ),
),

我要更新的字段在 mysql 数据库中被声明为 DATETIME,以防万一。

【问题讨论】:

  • 我现在看到在我的 Validation::date() 调用中传入的“日期”包括时间,我认为它不会接受时间。
  • NOT_EMPTY 规则不是一个常数吗?还是那所旧学校现在?

标签: validation cakephp cakephp-1.3


【解决方案1】:

我在 Cake 2.0 数据验证文档 (http://book.cakephp.org/2.0/en/models/data-validation.html#Validation::datetime) 中找到了以下信息:

Validation::datetime(array $check, mixed $dateFormat = 'ymd', string $regex = null)
This rule ensures that the data is a valid datetime format. A parameter (which can be an array) can be passed to specify the format of the date. The value of the parameter can be one or more of the following:

'dmy' e.g. 27-12-2006 or 27-12-06 (separators can be a space, period, dash, forward slash)
'mdy' e.g. 12-27-2006 or 12-27-06 (separators can be a space, period, dash, forward slash)
'ymd' e.g. 2006-12-27 or 06-12-27 (separators can be a space, period, dash, forward slash)
'dMy' e.g. 27 December 2006 or 27 Dec 2006
'Mdy' e.g. December 27, 2006 or Dec 27, 2006 (comma is optional)
'My'  e.g. (December 2006 or Dec 2006)
'my'  e.g. 12/2006 or 12/06 (separators can be a space, period, dash, forward slash)
 if no keys are supplied, the default key that will be used is 'ymd':

【讨论】:

    【解决方案2】:

    我不记得了,但我认为您必须使用生成的日期输入来构建自己的日期。

    !!- 擦洗那个 -!!

    ‘Mdy’ e.g. December 27, 2006 or Dec 27, 2006 (comma is optional)
    

    如果没有提供密钥,则将使用的默认密钥是“ymd”。

    我支持 SpawnCxy。

    【讨论】:

    • 好的,我可以试试看。我注意到的一件事是,如果我不验证日期正在正确更新,这让我认为您不需要自己建立日期字符串。加上这似乎比你必须做的更多的工作。蛋糕这么好吃。
    【解决方案3】:

    我在parameter list中找不到参数MDY,你可以试试Mdy

    【讨论】:

    • “MDY”用于视图的“dateFormat”参数,但验证看起来需要“Mdy”。
    • 我的意思是验证,而不是表单创建部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-05
    • 2017-01-25
    • 2013-06-30
    • 1970-01-01
    相关资源
    最近更新 更多