Validation Yii2将模型中的日期与函数进行比较

Validation Yii2将模型中的日期与函数进行比较,validation,date,model,compare,yii2,Validation,Date,Model,Compare,Yii2,我有以下规则的模型 public function rules() { return [ ['student_dob', function ($attr) { $curr_date = date('d-m-Y'); if(empty($this->student_adm_date)) { //$this->addError('student_dob',"Sel

我有以下规则的模型

public function rules()
{
  return [    
   ['student_dob', 
        function ($attr) {
            $curr_date = date('d-m-Y');
            if(empty($this->student_adm_date)) {
                //$this->addError('student_dob',"Select Admission date first"); 
                return true;
            }
            else {
              $dob = date('Y-m-d',strtotime($this->$attr));
              $adm = date('Y-m-d',strtotime($this->student_adm_date));
              $diff = $adm-$dob;
               if($diff <= 14)  {
            
                $this->addError('student_dob', "Birth date must be less than Admission date."); 
                return false;   
    
               }

               else
                return true;
            }
            
            },
        ]
    ];
}

你是在用一根针减去另一根针,我不确定这是否有效

$diff = $adm - $dob;
试一试


还有一件事,在验证函数中,您不需要
返回false
返回true尝试删除这些,我个人不知道它们是否有任何作用。

验证已在运行,但表单字段上未显示错误消息。。。
<?php   $form = ActiveForm::begin([
        'layout' => 'horizontal',
        'fieldConfig' => [
            'template' => "{label}\n{input}\n{error}",
        ],
    ]);
?>
$diff = $adm - $dob;
if(strtotime($this->$attr) <= strtotime($this->student_adm_date))
if(strtotime($this->$attr) - strtotime($this->student_adm_date) > 60 * 60 * 24 * 14)