尝试更新时清空加载数据 - Yii2

Empty load date whrn trying yo update - Yii2

碰巧我使用了 minDate 选项来停用当前日期之前的日期,以避免选择已经过去的日期。这在创建时非常适合我,但在更新中它不显示时间,只出现空的小部件,尽管在数据库中它显示日期已注册。我想知道可能发生了什么错误。

规则

[['inicio_clase', 'fin_clase'], 'default', 'value' => function () {
            return date(DATE_ISO8601);
        }],
        [['inicio_clase', 'fin_clase'], 'filter', 'filter' => 'strtotime', 'skipOnEmpty' => true],
        [['inicio_clase', 'fin_clase','seccion_id', 'materia_id', 'estado'], 'integer'],
        ['inicio_clase','compare','compareAttribute'=>'fin_clase','operator'=>'<','message'=>'La fecha de inicio debe ser menor que la fecha de finalización.'],
        ['fin_clase','compare','compareAttribute'=>'inicio_clase','operator'=>'>','message'=>'La fecha de fin no debe ser menor o igual que la fecha de inicio.'],

表格

<?php echo $form->field($model, 'fin_clase'(
                        DateTimeWidget::class,
                        [
                            'phpDatetimeFormat' => 'yyyy/MM/dd HH:mm',
                            'clientOptions' => [
                                'minDate' => new \yii\web\JsExpression('new Date()'),
                              ]
                        ]
                    ) ?>

您可能正在尝试编辑 fin_clase 设置为当前日期之前的日期的记录。 DateTimeWidget 设置为不允许日期早于当前日期,因此即使该值存储在模型中,它也无法显示过去的日期。

例如,如果今天是 2021-10-12 并且记录的 fin_clase 设置为 2021-10-11,则在编辑期间不会被填充。

为避免这种情况,您应该设置 minDate 以允许存储在模型中的实际值。

'minDate' => $model->isNewRecord  
    ? new \yii\web\JsExpression('new Date()')
    : new \yii\web\JsExpression("new Date({$model->fin_clase})"),

或者您可能希望在编辑记录时允许所有日期都是过去的。

'minDate' => $model->isNewRecord  
    ? new \yii\web\JsExpression('new Date()')
    : false,