【问题标题】:Laravel chosen-select retrieving old value from the formLaravel selected-select 从表单中检索旧值
【发布时间】:2018-04-30 01:55:46
【问题描述】:

我正在尝试从编辑表单上的表单中检索旧值,但似乎无法找到答案,因为世界上每个人都决定使用来自 laravelcollective/html 库的 Form::select。我正在尝试使用常规 HTML 来解决这个问题,但它无法从表单中检索旧值。

这是我的 HTML 代码。

                        <div class="col-xs-12 form-group">
                            <label class="control-label">Role*</label>
                            <select name="roles[]" data-placeholder="Choose a Role for this User..." class="chosen-select" multiple tabindex="4">
                                <option value="">Select</option>
                                @foreach ($roles as $key => $value)
                                    <option value="{{ $key }}" {{ in_array($key, old("roles")) ? "selected":"") }} >{{ $value }}</option>
                                @endforeach
                            </select>
                        </div>

这是我的控制器代码。

public function edit($id)
{
    $roles = Role::get()->pluck('name', 'id');
    $user = User::findOrFail($id);
    return view('admin.users.edit', compact('user', 'roles'));
}

我正在尝试将以下内容转换为常规 html

<div class="row">
            <div class="col-xs-12 form-group">
                {!! Form::label('role', trans('global.users.fields.role').'*', ['class' => 'control-label']) !!}
                {!! Form::select('role[]', $roles, old('role') ? old('role') : $user->role->pluck('id')->toArray(), ['class' => 'form-control select2', 'multiple' => 'multiple', 'required' => '']) !!}
                <p class="help-block"></p>
                @if($errors->has('role'))
                    <p class="help-block">
                        {{ $errors->first('role') }}
                    </p>
                @endif
            </div>
        </div>

【问题讨论】:

  • 旧值是当您提交表单并且表单中有错误时,它会使用旧的session 数据填充它。据我所知,您不会以这种方式使用它。您只能从数据库中获取。所以你不需要旧的功能。
  • 你的 $request->flash() 在哪里???

标签: laravel laravel-5.4 laravel-5.5 laravel-5.6 laravelcollective


【解决方案1】:

试试这个:

<div class="col-xs-12 form-group {{ $errors->has('role') ? 'has-error' : '' }}">
  <label for="role" class="control-label">Role *</label>

  <select class="form-control select2" name="role[]" id="role" required multiple>
    @foreach ($roles as $id => $label)
      <option value="{{ $id }}" @if (collect(old('role', $user->role->pluck('id') ?? []))->contains($id)) selected="selected" @endif>{{ $label }}</option>
    @endforeach
  </select>

  <span class="help-block">
  @if ($errors->has('role'))
    {{ $errors->first('role') }}
  @endif
  </span>
</div>

在控制器的更新/存储功能中,确保您使用内置验证方法,这将在任何错误时填充会话。

如果您有其他检查,请确保在重定向回来时返回输入。

return redirect()->back()
    ->withInput() // <- return input with redirect, will populate session
    ->with('error', 'Persist failed'); // <- optional 

【讨论】:

  • 我收到以下错误。 "在 null 上调用成员函数 pluck()"
  • 啊,您使用role 而不是roles 作为关系名称,在答案中更改了它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-09
  • 2018-12-06
  • 1970-01-01
相关资源
最近更新 更多