Select Selected value of a dropdown in laravel 5.4

倾然丶 夕夏残阳落幕 提交于 2019-12-06 05:27:35

问题


I have a dropdown called designation, where a user will select one of it, and after submitting it, if there is some error then I want to select the selected designation.

I am using this in laravel 5.4.

Controller

$info = DB::table("designation")
                    ->where('status','=',1)
                    ->pluck("name","id");

        return view('regUser.add',['check' => 'userList','designation' => $info]);

View Files

<div class="form-group {{ $errors->has('designation') ? ' has-error' : '' }}">
                            <label for="designation">Designation</label>
                            <select id="designation" name="designation" class="form-control">
                                <option value="">--- Select designation ---</option>
                                @foreach ($designation as $key => $value)

                                    <option value="{{ $key }}" />{{ $value }}</option>
                                @endforeach
                            </select>
                            @if ($errors->has('designation'))
                            <span class="help-block">
                                <strong>{{ $errors->first('designation') }}</strong>
                            </span>
                            @endif  
                        </div>

Now if the validator found some error then I want to select the previously selected one. How can i achive this in laravel 5.4.

After submiting the form it comes to addUserInformation function, where I validate the user information which have this piece of code

 public function addUserInformation(Request $request){
            $this->validate($request, [
                'name' => 'required|string|min:5',
                'email' => 'required|string|email|unique:users,email',
                'designation' => 'required|exists:designation,id',
                'password'     => 'required|min:6',
                'confirm_password' => 'required|same:password',
                'userimage' => 'required|image',
            ]);
            $selectedID = $request->input('designation');
}

回答1:


Larvel passes the inputs back on validation errors. You can use the old helper function to get the previous values of form. A simple comparison should do the trick.

<div class="form-group {{ $errors->has('designation') ? ' has-error' : '' }}">
    <label for="designation">Designation</label>
    <select id="designation" name="designation" class="form-control">
        <option value="">--- Select designation ---</option>
        @foreach ($designation as $key => $value)
            <option value="{{ $key }}" {{ old('designation') == $key ? 'selected' : ''}}>{{ $value }}</option>
        @endforeach
    </select>
    @if ($errors->has('designation'))
        <span class="help-block">
            <strong>{{ $errors->first('designation') }}</strong>
        </span>
    @endif
</div>



回答2:


You have to maintain the state of old input by using return redirect() like:

return redirect('form')->withInput();

Retrieving Old Data

To retrieve flashed input from the previous request, use the old method on the Request instance.

$oldDesignation = Request::old('designation');

and check it like:

@foreach ($designation as $key => $value)
  $selected = '';
  @if( $value == $oldDesignation )
    $selected = 'selected="selected"';
  @endif
  <option value="{{ $key }}" {{ $selected }} />{{ $value }}</option>
@endforeach

Reference



来源:https://stackoverflow.com/questions/44238536/select-selected-value-of-a-dropdown-in-laravel-5-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!