I am using jQuery (jui) date picker extension in Yii 2 form. I have a table in the database which stores the StartYear and EndYear. StartYear and EndYear are having varchar datatype.
For example: StartYear : June 2023 and EndYear : April 2024
I have configured the datepicker to show only months and year. Data is correctly getting saved in create action. But in the update action, incorrect values are getting displayed in StartYear and EndYear fields.
Below is the _form.php
<?php
use yiihelpersHtml;
use yiibootstrapActiveForm;
use yiijuiDatePicker;
use yiihelpersArrayHelper;
/* @var $this yiiwebView */
/* @var $model appmodelsAcademicyear */
/* @var $form yiiwidgetsActiveForm */
?>
<style type="text/css">
.ui-datepicker-calendar {
display: none;
}
</style>
<script>
$(document).ready(function() {
$('.picker').datepicker({
changeMonth: true,
changeYear: true,
dateFormat: 'MM yy',
onClose: function() {
var iMonth = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
var iYear = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
$(this).datepicker('setDate', new Date(iYear, iMonth, 1));
},
});
});
</script>
<?php $form = ActiveForm::begin()?>
<div class="row">
<div class="col-sm-5">
<?= $form->field($model, 'StartYear')->widget(DatePicker::classname(), [
//'language' => 'ru',
'options' => ['class' => 'form-control picker','readOnly'=>'readOnly'],
'clientOptions'=>['changeMonth'=>true,
'changeYear'=>true,
'dateFormat' => 'MM yy',
'readOnly'=>true]
]) ?>
</div>
</div>
<div class="row">
<div class="col-sm-5">
<?= $form->field($model, 'EndYear')->widget(DatePicker::classname(), [
//'language' => 'ru',
'options' => ['class' => 'form-control picker','readOnly'=>'readOnly'],
'clientOptions'=>['changeMonth'=>true,
'changeYear'=>true,
'dateFormat' => 'MM yy',
'readOnly'=>true]
]) ?>
</div>
</div>
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
<?php ActiveForm::end(); ?>