M
M
Monitorkin2017-04-28 09:55:12
Yii
Monitorkin, 2017-04-28 09:55:12

Why is date not being saved from DatePicker Yii2?

in the shape of:

<?=
  $order_form->field($order_model, 'date_delivery')->widget(DatePicker::classname(), [
    'language' => 'ru', 'dateFormat' => 'dd-MM-yy',
    'options'=>['class'=>' form-control input-sm'],					
  ])
?>

in model:
public function rules() {
    return [
      ...
      [['date_delivery'], 'date','format'=>'dd-MM-yy','timestampAttribute'=>'date_delivery'],
      [['date_delivery'], 'default', 'value' => null],
      ...
    ];
  }

date_delivery is stored in the DATE format in the database, when saving errors, validation is not issued, but 0000-00-00 appears in the database. Where is the mistake? Thank you!

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
Maxim Timofeev, 2017-04-28
@webinar

Check in what format the data goes to the database, you can see it in the debug.
Next, check in what format the data is stored in the database, as I understand it, 0000-00-00 is a clear sign that this is a year month day or year day month (but the first option is more likely)
Next, bring the data that the widget sends to the desired format and make sure that there is no additional logic in the model that changes the format.
Or vice versa, in the model, in beforeSave, change the format.
I can also draw your attention to this component:
demos.krajee.com/datecontrol
Which actually solves the issue of various formats in the database and on the front in widgets.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question