在 laravel 5.4 中选择下拉列表的选定值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44238536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:03:19  来源:igfitidea点击:

Select Selected value of a dropdown in laravel 5.4

phplaravellaravel-5.4

提问by Saroj

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.

我有一个名为 designation 的下拉菜单,用户将在其中选择其中一个,提交后,如果出现错误,我想选择所选的 designation。

I am using this in laravel 5.4.

我在 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.

现在,如果验证器发现一些错误,那么我想选择之前选择的错误。我怎样才能在 Laravel 5.4 中实现这一点。

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

提交表单后,它涉及到addUserInformation功能,在那里我验证具有这段代码的用户信息

 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');
}

回答by Sandeesh

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

Larvel 将输入传回验证错误。您可以使用old辅助函数获取表单的先前值。一个简单的比较应该可以解决问题。

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

回答by Mayank Pandeyz

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

您必须使用以下方法维护旧输入的状态return redirect()

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

Retrieving Old Data

检索旧数据

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

要从前一个请求中检索闪现的输入,请在 Request 实例上使用旧方法。

$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

参考