Laravel 4 Form :: selectMonth month to month

How can i add option to 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 add parameter 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 to default or selected as month placeholder.

0


source to share


2 answers


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, this can 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 that you can also extend the FormBuilder class and create a new one Form::selectMonth()

.

+2


source


create a new file inside app /macros.php and copy the pasteurized line of that line.

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);
});

      



macros.php required in app / start / global.php

+1


source







All Articles