【问题标题】:Yii2 compare date in model with functionYii2 将模型中的日期与函数进行比较
【发布时间】:2014-12-09 07:03:13
【问题描述】:

我有以下规则的模型

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;
            }
            
            },
        ]
    ];
}

此规则效果很好,但错误消息不会出现在视图页面上,我的表单视图在下方

<?php   $form = ActiveForm::begin([
    'layout' => 'horizontal',
    'fieldConfig' => [
        'template' => "{label}\n{beginWrapper}\n{input}\n{error}\n{endWrapper}",
        'horizontalCssClasses' => [
            'label' => 'col-sm-4',
            'offset' => 'col-sm-offset-4',
            'wrapper' => 'col-sm-8',
            'error' => '',
            'hint' => '',
        ],
    ],
]);
?>
<div class="row-left">
            <?= $form->field($info, 'student_dob', ['template' => "{label} {input} <span class='status'>&nbsp;</span> {error}"])->widget(yii\jui\DatePicker::className(),
                    [
            'model'=>$info, 
            'attribute'=>'student_dob',
            'value'=>'',
                        'clientOptions' =>[
                        'dateFormat' => 'dd-mm-yyyy',
                        'changeMonth'=> true,
                        'changeYear'=> true,
                        'autoSize'=>true,
                        'showOn'=> "button",
            'yearRange'=>'1900:'.(date('Y')+1),
                        'buttonImage'=> Yii::$app->homeUrl."images/calendar.png",
                        'htmlOptions'=>[
                        'style'=>'width:250px;',
                          'class'=>'form-control',
                         ]]]); ?> 
            
        </div>
<?php ActiveForm::end(); ?>

我想将student_dobstudent_adm_datestudent_dob &lt; student_adm_date 规则验证进行比较,表单视图页面上也会出现错误。


解决方案

只需删除 horizontalCssClasses 并更改 template 值,例如,

<?php   $form = ActiveForm::begin([
        'layout' => 'horizontal',
        'fieldConfig' => [
            'template' => "{label}\n{input}\n{error}",
        ],
    ]);
?>

【问题讨论】:

    标签: validation date model compare yii2


    【解决方案1】:

    你正在从另一个中减去一个刺,我不确定这是否有效

    $diff = $adm - $dob;
    

    试试

    if(strtotime($this->$attr) <= strtotime($this->student_adm_date))
    

    或将其更改为 calcualte 14 天...虽然不确定您可以在某人出生 14 天后注册什么。 你可以计算

    if(strtotime($this->$attr) - strtotime($this->student_adm_date) > 60 * 60 * 24 * 14)
    

    还有一点,在验证功能中你不需要return false;return true; 试试去掉这些,我个人不知道他们有没有什么作用。

    【讨论】:

    • 验证已经开始,但是错误信息没有出现在表单域上...
    猜你喜欢
    • 2015-10-12
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多