Reputation: 11
How can I prepend an option into laravel
{{ Form::selectMonth('month') }}
Result
<select name="month">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
I want to prepend an option value=0 like this
<select class="form-control" name="month">
<option value="0">Month</option>
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
And set the default or selected to the Month placeholder.
Upvotes: 0
Views: 1856
Reputation: 87789
You can't, so you'll have to create your own array:
$months = array(0 => 'Month');
foreach (range(1, 12) as $month)
{
$months[$month] = strftime($format, mktime(0, 0, 0, $month, 1));
}
pass it to your views:
return View::make('viewname')->with('months', $months);
and use Form::select()
:
{{ Form::select('month', $months) }}
As a form macro it could be:
Form::macro('selectMonthWithDefault', function($name, $options = array(), $format = '%B')
{
$months = array(0 => 'Month');
foreach (range(1, 12) as $month)
{
$months[$month] = strftime($format, mktime(0, 0, 0, $month, 1));
}
return Form::select($name, $months);
});
And don't forget you can also extend the FormBuilder class and create a new Form::selectMonth()
.
Upvotes: 2
Reputation: 11
create new file inside app/macros.php and copy paster this line of codes
Form::macro('selectMonths', function($name, $options = array(), $format = '%B')
{
$months = array(0 => 'Month');
foreach (range(1, 12) as $month)
{
$months[$month] = strftime($format, mktime(0, 0, 0, $month, 1));
}
return Form::select($name, $months, null, $options);
});
require macros.php in app/start/global.php
Upvotes: 1