Yii2 - 根据3个输入字段验证日期,如果日期无效则显示错误消息

时间:2016-04-15 17:59:45

标签: yii yii2

如果日期来自3个输入字段,如何验证日期并显示错误消息?这是字段:

<div class="col-xs-12 reg2-div">
<div class="col-xs-3 reg2-db-div">
<?php echo $form->field($model_data_step_2, 'recipient_birth_month')->textInput() ?>
</div>
<div class="col-xs-1 reg2-db-div">
<span style="font-size: 50px;">/</span>
</div>
<div class="col-xs-3 reg2-db-div">
<?php echo $form->field($model_data_step_2, 'recipient_birth_day')->textInput() ?>
</div>
<div class="col-xs-1 reg2-db-div">
<span style="font-size: 50px;">/</span>
</div>
<div class="col-xs-4 reg2-db-div">
<?php echo $form->field($model_data_step_2, 'recipient_birth_year')->textInput() ?>
 </div>

1 个答案:

答案 0 :(得分:0)

在模型中编写自定义规则

我正在写一个虚拟模型

class SignupForm extends Model
{
    public function rules()
    {
        return [
            ['recipient_birth_month', 'checkMonth'],
            ['recipient_birth_day', 'checkDay'],
            ['recipient_birth_year', 'checkYear'],

            // other rules
        ];
    }

    public function checkMonth($attribute, $params)
    {
        //  the error is triggered if >12
        if($this->recipient_birth_month > 12 )
            $this->addError($attribute, Yii::t('user', 'You entered an invalid month.'));
    }
    public function checkMonth($attribute, $params)
    {
        //  the error is triggered
        if($this->recipient_birth_day> 31 )
            $this->addError($attribute, Yii::t('user', 'You entered an invalid Day.'));
    }
    public function checkMonth($attribute, $params)
    {
        //  the error is triggered
        if($this->recipient_birth_year > 2016 )
            $this->addError($attribute, Yii::t('user', 'You entered an invalid Year.'));
    }
}
希望这会有所帮助 如果您只想要一条错误消息,而不是像这样创建一个自定义验证

class SignupForm extends Model
    {
        public function rules()
    {
        return [
            ['recipient_birth_year', 'allinonedatecheck'],
            // other rules
        ];
    }

    public function allinonedatecheck($attribute, $params)
    {
        //  the error is triggered if >12
        if($this->recipient_birth_month > 12 )
            $this->addError($attribute, Yii::t('user', 'You entered an invalid month.'));

        //  the error is triggered
        if($this->recipient_birth_day > 31 )
            $this->addError($attribute, Yii::t('user', 'You entered an invalid Day.'));

        //  the error is triggered
        if($this->recipient_birth_year > 2016 )
            $this->addError($attribute, Yii::t('user', 'You entered an invalid Year.'));
    }
}

现在,即使日期错误,错误信息也只会显示在@ recipient_birth_year

  

如果您想检查日期是否正确,可以使用yii2   验证

class SignupForm extends Model
    {
        public function rules()
    {
        return [
            ['recipient_birth_year', 'datecheck'],
            // other rules
        ];
    }

    public function datecheck($attribute, $params)
    {

        $d =new \yii\validators\DateValidator ();
        $d->format = 'MM/dd/yyyy'; // date in ICU format
        if(!$d->validate($this->recipient_birth_month ."/".$this->recipient_birth_day."/".$this->recipient_birth_year))
        {
             $this->addError($attribute, Yii::t('user', 'You entered an invalid Date.'));
        }
    }
}