Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 DatePicker

Tags:

jquery-ui

yii2

I'm running a datePicker for a date of birth field in Yii2. I'm trying to display the year also. I've checked the JQuery docs http://jqueryui.com/datepicker/#dropdown-month-year and I thought I would be able to achieve this with changeYear. But it doesn't seem to work. What's the way to do this? This is my current code.

<?= $form->field($model, 'date_of_birth')->widget(DatePicker::className(),[
   'name' => 'date_of_birth',
   'language' => 'en-GB',
   'dateFormat' => 'yyyy-MM-dd',
   'options' => [
      'changeMonth' => true,
      'changeYear' => true,
      'yearRange' => '1996:2099',
      'showOn' => 'button',
      'buttonImage' => 'images/calendar.gif',
      'buttonImageOnly' => true,
      'buttonText' => 'Select date'
    ],
]) ?>
like image 482
Jonnny Avatar asked Jan 24 '15 20:01

Jonnny


2 Answers

You should simply use clientOptions instead of options :

'clientOptions' => [
    'changeMonth' => true,
    'yearRange' => '1996:2099',
    'changeYear' => true,
    'showOn' => 'button',
    'buttonImage' => 'images/calendar.gif',
    'buttonImageOnly' => true,
    'buttonText' => 'Select date'
],
like image 161
soju Avatar answered Nov 15 '22 12:11

soju


You can simply use HTML5 date type. That is so simple. You dont need to add widgets:

<?= $form->field($model, 'date_of_birth')->textField(['type' => 'date']);?>

This will add the date picker.

like image 38
Awais Mustafa Avatar answered Nov 15 '22 10:11

Awais Mustafa