yii2日期验证无效

时间:2016-01-21 04:52:53

标签: validation yii2 yii2-advanced-app

在模型中我有开始和结束日期,我需要比较开始和结束日期。我为格式添加了验证规则,并为比较添加了自定义验证规则

        [['start_date','end_date'], 'date', 'format' => 'yyyy-M-d'],
        ['start_date','validateDates'],


    public function validateDates(){
       if(strtotime($this->end_date) <= strtotime($this->start_date)){
        $this->addError('start_date','Please give correct Start and End dates');
        $this->addError('end_date','Please give correct Start and End dates');
       }
     }

我也尝试过格式验证

   [['start_date','end_date'], 'date', 'format' => 'php:Y-m-d'],

在视图文件中我定制了设计的开始和结束日期

                    <?= $form->field($quote, 'start_date',['template' => '{label}{input}<p class="help-block help-block-error">{hint}</p><div class="dtBox"></div>'])->textInput(['data-field' => 'date'])->label("Arrival Date", [
                                'class' => 'control-label quote-layout-text',
                        ]); ?>


                    <?= $form->field($quote, 'end_date',['template' => '{label}{input}<p class="help-block help-block-error">{hint}</p><div class="dtBox"></div>'])->textInput(['data-field' => 'date'])->label("Departure Date", [
                                'class' => 'control-label quote-layout-text',
                        ]); ?>

我将开始和结束日期验证添加到必需,然后我获得了所需字段的验证消息。但我没有获得格式的验证消息比较

1 个答案:

答案 0 :(得分:0)

您的字段模板中似乎没有{error}标记。尝试添加它。