yii中的日期验证规则

时间:2014-03-07 08:02:04

标签: php validation yii

我在验证日期规则方面遇到问题,当我输入无效日期(如'xxxx')时,我没有运行验证程序,但它运行其他带有所需验证程序的字段。

表格

?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'post-form',
    'enableAjaxValidation'=>false,
    'enableClientValidation'=>true,
)); ?>

...

<div class="row">
        <?php echo $form->labelEx($model,'fromDate'); ?>
        <?php echo $form->textField($model,'fromDate'); ?>
        <?php echo $form->error($model,'fromDate'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'toDate'); ?>
        <?php echo $form->textField($model,'toDate'); ?>
        <?php echo $form->error($model,'toDate'); ?>
    </div>

帖子模型中的规则

public function rules()
    {
    return array(
    array('fromDate, toDate', 'date', 'format'=>'dd-mm-yyyy H:m:s', 'allowEmpty'=>false),
            array('subject', 'required'),
                    );
...

    }

提前感谢您的帮助

3 个答案:

答案 0 :(得分:3)

我发现Yii的CDateValidator特别灵活,我通常会沿着创建自定义验证规则的路线前进:

将此添加到您的模型:

public function isValidDate($attribute, $params)
{
    if(!strtotime($this->$attribute))
    {
        $this->addError($attribute, $attribute . ' was not a valid date');
    }
}

然后将自定义验证器分配给rules()数组中的属性:

array('fromDate, toDate', 'isValidDate'),

您可以对其进行扩展,以确保日期在合理的时间范围内,toDatefromDate之后等。

答案 1 :(得分:1)

您可以使用Yii小部件

   <div class="row">
    <?php
    echo $form->labelEx($model, 'fromDate');

    $this->widget('CJuiDateTimePicker', array(
        'model' => $model, //Model object
        'attribute' => 'fromDate', //attribute name
        'mode' => 'date', //use "time","date" or "datetime" (default)
        'options' => array(
            'dateFormat' => "dd/mm/yy",
            'defaultDate' => "new Date()",
        ), // jquery plugin options
        'htmlOptions' => array(
            'id' => 'fromDate',
        ),
    ));
    ?>

</div>

并在行动中:

 if ($model->fromDate != '') {
     $temp = strtotime(str_replace('/', '-', $model->fromDate));
     $temp = date('Y-m-d', $temp);
     $model->fromDate = $temp;
     }

答案 2 :(得分:0)

验证规则在CDateTimeParse.php

中解释
 * Pattern |      Description
 * ----------------------------------------------------
 * d       | Day of month 1 to 31, no padding
 * dd      | Day of month 01 to 31, zero leading
 * M       | Month digit 1 to 12, no padding
 * MM      | Month digit 01 to 12, zero leading
 * MMM     | Abbreviation representation of month (available since 1.1.11; locale aware since 1.1.13)
 * MMMM    | Full name representation (available since 1.1.13; locale aware)
 * y       | 4 year digit, e.g., 2005 (available since 1.1.16)
 * yy      | 2 year digit, e.g., 96, 05
 * yyyy    | 4 year digit, e.g., 2005
 * h       | Hour in 0 to 12, no padding
 * hh      | Hour in 00 to 12, zero leading
 * H       | Hour in 0 to 23, no padding
 * HH      | Hour in 00 to 23, zero leading
 * m       | Minutes in 0 to 59, no padding
 * mm      | Minutes in 00 to 59, zero leading
 * s       | Seconds in 0 to 59, no padding
 * ss      | Seconds in 00 to 59, zero leading
 * a       | AM or PM, case-insensitive (since version 1.1.5)
 * ?       | matches any character (wildcard) (since version 1.1.11)

 * For example, to parse a date string '21/10/2008', use the following:
 * $timestamp=CDateTimeParser::parse('21/10/2008','dd/MM/yyyy');