模型验证日期格式

时间:2015-12-07 14:22:55

标签: yii2

我的模型在验证输入日期时,转换格式类型会导致麻烦。

<?php $form = yii\widgets\ActiveForm::begin([
    'options' => ['class' => ''],
    'fieldConfig' => ['template' => "{label}\n{input}\n{hint}\n{error}"]
]); ?>

<div class="col-md-4"> 
    <?= $form->field($model, 'startDate')->widget(DatePicker::classname(), 
          [
            'language'      => 'pt-BR',
            'dateFormat'    => 'dd/MM/yyyy',

            'clientOptions' => [
                'yearRange'     => '2005:+0', 
                'defaultDate'   => date('d/m/Y', strtotime('-7days'))
            ],
            'options'       => [
                'class'         => 'form-control',
                'maxlength'     => '10',
                'placeholder'   => 'Data inicial',
                ]
          ]
    )->label(false) ?>
</div>
<div class="col-md-4"> 
    <?= $form->field($model, 'endDate')->widget(DatePicker::classname(), 
          [
            'language'      => 'pt-BR',
            'dateFormat'    => 'dd/MM/yyyy',
            'clientOptions' => [
                'yearRange'     => '2005:+0', 
                'defaultDate'   => date('d/m/Y')
            ],
            'options'       => [
                'class'         => 'form-control',
                'maxlength'     => '10',
                'placeholder'   => 'Data final',
                ]
          ]
    )->label(false) ?>
</div>
<div class="col-md-4"> 
    <?= Html::submitButton('Filtrar', ['class' => 'btn btn-primary']) ?>
    <?php yii\widgets\ActiveForm::end(); ?>
</div>

模特:

<?php

namespace frontend\models;

use yii\base\Model;
use Yii;

class Analysis extends Model
{
    public $startDate;
    public $endDate;

    public function rules()
    {
        return [
            ['startDate', 'required'],
            ['startDate', 'date', 'format' => 'dd/MM/yyyy'],
            ['startDate', 'checkDateToday'],
            ['startDate', 'checkGAMinDate'],

            ['endDate', 'required'],
            ['endDate', 'date', 'format' => 'dd/MM/yyyy'],
            ['endDate', 'checkDateToday'],
            ['endDate', 'checkGAMinDate'],
            ['endDate', 'checkDateGreaterThan']
        ];
    }

    public function checkDateToday($attribute, $params)
    {
        if($this->$attribute <= date('d/m/Y')){
            return true;
        } else {
        var_dump($this->$attribute);
            $this->addError($attribute, $attribute . ' não pode ser maior que hoje: ' . date('d/m/Y'));
        }
    }    

    public function checkGAMinDate($attribute, $params)
    {
        if($this->$attribute > date('01/01/2005')){
            return true;
        } else {
            $this->addError($attribute, $attribute . ' não pode ser menor que 01/01/2005');
        }
    }

    public function checkDateGreaterThan($attribute, $params)
    {
        if($this->endDate < $this->startDate){
            return true;
        } else {
            $this->addError($attribute, 'Data final não pode ser maior que data incial.');
        }
    }

    public function attributeLabels()
    {
        return [
            'startDate' => Yii::t('app', 'Data inicial'),
            'endDate'   => Yii::t('app', 'Data final'),
        ];
    }
}

当我输入“25/12/2015”('d / m / Y')之类的日期时,模型在验证后返回:“2015年12月25日”。什么出错了?

1 个答案:

答案 0 :(得分:2)

问题是Datepicker's value property。正如文档所述,它使用Yii::$app->formatter->asDate()来生成值。

由于该值始终为字符串,因此必须为valid date format。正如您所看到的,没有dd/mm/yyyy格式,但有一个mm/dd/yyyy

我找不到取消此格式的方法,因此您可以更改格式,或者,如果要维护此格式,只需将模型的值更改为其他有效格式(IE:{{1 }}),只能正确打印。

表格之前有这样的东西:

dd-mm-yyyy