Creating a Select Range Field

Problem

You want a select box with a range of values.

Solution

Use the Form::selectRange() method.

Just specify the start and end of the numeric range.

{{ Form::selectRange('number', 10, 15) }}

This produces options 10, 11, 12, 13, 14, and 15.

<select name="number">
  <option value="10">10</option>
  <option value="11">11</option>
  <option value="12">12</option>
  <option value="13">13</option>
  <option value="14">14</option>
  <option value="15">15</option>
</select>

Add a fourth option for the default value and a fifth option for any additional attributes for the select field.

{{ Form::selectRange('number', 10, 15, 13, ['class' => 'field']) }}

Now #13 is selected and the select field has a class attribute.

<select class="field" name="number">
  <option value="10">10</option>
  <option value="11">11</option>
  <option value="12">12</option>
  <option value="13" selected="selected">13</option>
  <option value="14">14</option>
  <option value="15">15</option>
</select>

Discussion

This calls Form::select() internally.

See Creating a Select Box Field for more details.

comments powered by Disqus