Pathros
Pathros

Reputation: 10740

Laravel 4 How to show selected values in a dropdown menu by using the input::old() method?

i am stuck with this.

I am using Laravel 4 and the select2 dropdown menu plugin.

I have a form where the user may choose one or more options from a dropwdown menu (the select2 plugin).

If the validation fails, then the user is returned again to the form. As for the correct inputs, i want the form to keep the selected values from the dropdown menus.

This is going well for one dropdown menu where the user must choose only one option. The code i've got is the following:

          <!-- Tipo de evento -->
          <div class="form-group <?php if($errors->has('tipo_evento')){echo "has-error";}else if(Input::old('tipo_evento')) echo "has-success"; ?>">
            <label for="tipo_evento" class="col-lg-2 control-label">Tipo</label>
            <div class="col-lg-10">
              <select multiple="" 
                class="select2 req_place form-control" placeholder="Elija" data-select-search="true"
                id="tipo_evento" name="tipo_evento" ><!-- required="" -->
                  @foreach($tipo_evento as $key1=>$datum)
                      @foreach($datum as $key2=>$value)
                          @if($key2=='id')
                              <option value="{{$value}}" {{ (Input::old('tipo_evento')==$value) ? ' selected="" ' : ''}}>
                          @elseif($key2=='nombre')
                              {{$value}}</option>
                          @endif
                      @endforeach
                  @endforeach
              </select>
              @if($errors->has('tipo_evento'))
                  <p style="color: red;">{{$errors->first('tipo_evento')}}</p>
              @endif                      
            </div>
          </div>

This is going fine, when the form is returned (in case of errors) the selected option remains selected.

However, for the case of multiple select, that is to say, when the user may choose one or more options, nothing happens, none of the option remains selected. Here is my code:

          <!-- Lugar en donde fue el evento -->
          <div class="form-group <?php if($errors->has('lugar')){echo "has-error";}else if(Input::old('lugar')) echo "has-success"; ?>">
            <label for="select" class="col-lg-2 control-label">Lugar(es)</label>
            <div class="col-lg-10">
              <select multiple="" 
                class="select2 req_place form-control" placeholder="Elija" data-select-search="true"
                id="lugar" name="lugar[]" ><!-- required="" -->
                  @foreach($lugares as $key1=>$datum)
                      @foreach($datum as $key2=>$value)
                          @if($key2=='id')
                              <option value="{{$value}}" {{ (Input::old('lugar[]')==$value) ? ' selected="" ' : ''}}>
                          @elseif($key2=='lugar')
                              {{$value}}</option>
                          @endif
                      @endforeach
                  @endforeach
              </select>
              <p style="color:blue;">{{Input::old('lugar[]')}}</p>
              @if($errors->has('lugar'))
                  <p style="color: red;">{{$errors->first('lugar')}}</p>
              @endif                      
            </div>
          </div>

I have tried this:

<option value="{{$value}}" {{ (Input::old('lugar[$value]')==$value) ? ' selected="" ' : ''}}>

And this:

<option value="{{$value}}" {{ (Input::old('lugar')==$value) ? ' selected="" ' : ''}}>

And so on...

I have found a hint here but i am trying to find out how to put the variable a not a constant or name ...

Any ideas to fix this?

Upvotes: 1

Views: 2224

Answers (3)

Pawel Bieszczad
Pawel Bieszczad

Reputation: 13335

Try (Input::old('lugar.' . $key1) or (Input::old('lugar.' . $key2), I'm not really sure what you are doing there. If that doesnt help, could you show the var_dump of Input::old('lugar') and $lugares

Edit:

<!-- Lugar en donde fue el evento -->
<div class="form-group <?php if($errors->has('lugar')){echo "has-error";}else if(Input::old('lugar')) echo "has-success"; ?>">
    <label for="select" class="col-lg-2 control-label">Lugar(es)</label>
    <div class="col-lg-10">
        <select multiple class="select2 req_place form-control" placeholder="Elija" data-select-search="true" id="lugar" name="lugar[]" >
            @foreach($lugares as $key1=>$datum)
                @foreach($datum as $key2=>$value)
                    @if($key2=='id')
                        <option value="{{$value}}" @if(is_array(Input::old('lugar')) && in_array($value, Input::old('lugar'))){{' selected'}}@endif>
                            @elseif($key2=='lugar')
                                {{$value}}</option>
                    @endif
                @endforeach
            @endforeach
        </select>
        <p style="color:blue;">{{Input::old('lugar[]')}}</p>
        @if($errors->has('lugar'))
            <p style="color: red;">{{$errors->first('lugar')}}</p>
        @endif
    </div>
</div>

Upvotes: 2

user1669496
user1669496

Reputation: 33118

There is some unnecessary code here which can be cleaned up quite easily.

Build out your options array in your controller to save all the clutter and mess in your view. That's not the best place to handle that. In your controller, it might look like this...

$options = [];
foreach($lugares as $datum) {
    foreach($datum as $value) {
        $options[$value['id']] = $value['lugar'];
    }
}

And be sure to pass $options into your view. Then your view becomes very easy!

{{ Form::select('lugar[]', $options, Input::old('lugar'), array('multiple' => true, 'class' => 'select2 req_place form-control', 'placeholder' => 'Elija', 'data-select-search' => 'true', 'id' => 'lugar')) }}

Upvotes: 0

The Alpha
The Alpha

Reputation: 146269

You may try this:

{{ Form::select('lugar[]', $tipo_evento, Input::old('lugar[]'), ['multiple' => true]) }}

No need to do a loop manually.

Upvotes: 1

Related Questions