1

如何在我的 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,以防万一。

4

3 回答 3

1

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

!!- 擦洗那个 - !!

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

如果未提供任何键,则将使用的默认键是“ymd”。

我和 SpawnCxy 在这方面。

于 2010-07-20T22:02:48.647 回答
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':
于 2014-05-12T14:58:14.863 回答
0

MDY我在参数列表中没有找到参数。你可以试试Mdy

于 2010-07-21T00:38:05.217 回答