I have a table with a for_date column stored as an integer in the database. To show for_date using the DateTime format, I used ActiveForm with code:
<?= $form->field($model, 'for_date')->textInput([ 'class' => 'form-control datepicker', 'value' => $model->for_date ? date(Yii::$app->params['dateFormat'], $model->for_date) : date(Yii::$app->params['dateFormat'], time()), 'placeholder' => 'Time']) ->label('Attendance Date') ?>
But when I create and save, Yii informed This field must be integer
In the model file, I had 2 functions below for conversion before validation, but this is still an error.
public function beforeValidate(){ if(!is_int($this->for_date)){ $this->for_date = strtotime($this->for_date); $this->for_date = date('d/M/Y', $this->for_date); } return parent::beforeValidate(); } public function afterFind(){ $this->for_date = \Yii::t('app', Yii::$app->formatter->asDate($this->for_date)); $this->for_date = date('d/M/Y', $this->for_date); return parent::afterFind(); }
How can I do it right to save to the database using an integer?
source share