I have a table with column for_date
saved by type integer in database.
In order to show for_date
with format DateTime, 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 model file, I had 2 functions below to convert before validate, but it's still 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 make it right to save into database with integer?
I found solution. In the Model Search (my case is AttendanceSearch.php
), find rules function and move for_date
from line have integer
to below lines have safe
My Code:
public function rules()
{
return [
[['id', 'user_id', 'project_id', 'commit_time', 'workload_type'], 'integer'],
[['comment', 'for_date'], 'safe'],
];
}