Widget for Yii2, created selectbox field with years.
Source ¶
Source is on GitHub at et-soft/yii2-widget-select-year
Install ¶
Either run
$ php composer.phar require et-soft/yii2-widget-select-year "*"
or add
"et-soft/yii2-widget-select-year": "*"
to the `
require`
section of your composer.json
file.
Examples ¶
Show selectbox with values from 2015 (current year) to 1995 year (-20 years from current year):
<?php echo $form->field($model, 'year')->widget(etsoft\widgets\YearSelectbox::classname(), [
'yearStart' => 0,
'yearEnd' => -20,
]);
?>
Show selectbox with values from 2005 (current year - 10 years) to 2025 year (+ 10 years from current year):
<?php echo $form->field($model, 'year')->widget(etsoft\widgets\YearSelectbox::classname(), [
'yearStart' => -10,
'yearEnd' => 10,
]);
?>
Show selectbox with fix values from 2000 to 2010 years:
<?php echo $form->field($model, 'year')->widget(etsoft\widgets\YearSelectbox::classname(), [
'yearStart' => 2000,
'yearStartType' => 'fix',
'yearEnd' => 2010,
'yearEndType' => 'fix',
]);
?>
Question
Is there any benefit in using this over just using range function? I know range could do the above and also allow you to step the years i.e. put every 1/ 2 / 5 / 10 years etc.
Here is an example of what i am referring to
//fixed $form->field($model, 'year')->dropDownList(range(2000, 2010)); //dynamic end with fixed start $form->field($model, 'year')->dropDownList(range(date(Y) - 10, date(Y))); //year stepped by 5 $form->field($model, 'year')->dropDownList(range(1900, 2015, 5));
the above code has some bugs, so working version is here:
<?= $form->field($model, 'start_year')->dropDownList(array_combine(range(date(Y) - 30, date(Y)), range(date(Y) - 30, date(Y))), ['prompt'=>'Please select a year']);?>
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.