I would like to create a field in Laravel with a select range attribute
{{ Form::selectRange('number', 1, 1500000) }}
I would like the range to be between 1 and 1500000 with an increment step size of 50000
Currently when I create this, it just creates a super long select field with increments of 1
I did it using straight php, because AFAIK laravel doesnt support steps with selectrange so far.
<select name="min_price" class="form-control">
<?php for ($i = 1; $i <= 15; $i++) : ?>
<option value="<?php echo $i*10000; ?>"><?php echo number_format($i*10000); ?></option>
<?php endfor; ?>
e(Input::get('min_price'))</select>
I created a Form macro for this, basically extending the existing selectRange macro:
class FormBuilder extends FB
{
public function selectRangeWithInterval($name, $start, $end, $interval, $default = null, $attributes = [])
{
if ($interval == 0) {
return $this->selectRange($name, $start, $end, $default, $attributes);
}
$items = [];
$startValue = $start;
$endValue = $end;
if ($interval < 0) {
$interval *= -1;
}
if ($start > $end) {
if ($interval > 0) {
$interval *= -1;
}
$startValue = $end;
$endValue = $start;
}
for ($i=$startValue; $i<$endValue; $i+=$interval) {
$items[$i . ""] = $i;
}
$items[$endValue] = $endValue;
if (!in_array($default, $items)) {
$items[$default] = $default;
}
return $this->select($name, $items, $default, $attributes);
}
}
This can then be used in your view something like this:
{{ Form::selectRangeWithInterval('weightOfSackOfFeathers', 0, 7500, 150, null, ['class' => 'form-control input-xs']) }}