Creating a Month Selection Field

Problem

You want a select box with the 12 months listed.

Solution

Use the Form::selectMonth() method.

You can just call it with the field name.

{{ Form::selectMonth('month') }}

This produces twelve options.

<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>

Discussion

Use a second option to specify the default month. A third option will apply additional attributes to the select field.

{{ Form::selectMonth('month', 7, ['class' => 'field']) }}

This produces the list of months with July selected and the select field has a class attributes.

<select class="field" 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" selected="selected">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>
comments powered by Disqus