【问题标题】:Laravel keep chosen select valueLaravel 保持选择的选择值
【发布时间】:2015-09-07 18:36:20
【问题描述】:

我正在 Laravel 中构建一个使用 jQuery Chosen 插件的应用程序。当用户提交表单时,我使用请求来验证表单。但是,如果他们被重定向回来,例如如果他们错过了必填字段,则选择的选择字段不会保留其值。

我的代码如下。我错过了什么?

控制器

/**
 * Show the form for creating a new resource.
 *
 * @return Response
 */
public function create()
{
    return view('admin.newContractor')
        ->with('timesheetTemplates', $this->timesheet->listTemplates())
        ->with('supervisors', $this->user->getUsersByRole($this->role->findRoleByName('Supervisor')->id)->lists('email', 'id'))
        ->with('contractorRoleId', $this->role->findRoleByName('Contractor')->id)
        ->with('rateFields', $this->rate->all())
        ->with('miscFields', $this->misc->all())
        ->with('workTypes', $this->timesheet->getTypeWork());
}

查看

{!! Form::open([]) !!}
...

    {!! Form::label('supervisors', 'Assign Supervisor(s)') !!}
    {!! Form::select('supervisors[][supervisor_id]', $supervisors, null, ['class' => 'chosen-select', 'multiple']) !!}

... 
{!! Form::close([]) !!}

请求

<?php

namespace App\Http\Requests\Admin;

use App\Http\Requests\Request;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Validation\Factory as ValidatorFactory;
use String;

class CreateUserRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'first_name'        => 'required',
            'last_name'         => 'required',
            'email'             => 'required|unique:user,email',
        ];
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function validator(ValidatorFactory $factory)
    {
        if(!$this->get('status'))
        {
            $this->merge(['activation_code' => String::random(30)]);
        }

        return $factory->make($this->input(), $this->rules(), $this->messages());
    }
}

编辑:$this-&gt;rate-&gt;all() 的结果是:

Collection {#315 ▼
#items: array:4 [▼
  0 => Rate {#316 ▼
    #table: "rate"
    #fillable: array:3 [▶]
    +timestamps: true
    #connection: null
    #primaryKey: "id"
    #perPage: 15
    +incrementing: true
    #attributes: array:6 [▼
      "id" => 1
      "cust_id" => 1
      "field" => "hourly_rate"
      "name" => "Hourly Rate"
      "created_at" => "2015-09-07 08:11:46"
      "updated_at" => "2015-09-07 08:11:46"
    ]
    #original: array:6 [▶]
    #relations: []
    #hidden: []
    #visible: []
    #appends: []
    #guarded: array:1 [▶]
    #dates: []
    #dateFormat: null
    #casts: []
    #touches: []
    #observables: []
    #with: []
    #morphClass: null
    +exists: true
    +wasRecentlyCreated: false
  }
  1 => Rate {#317 ▶}
  2 => Rate {#318 ▶}
  3 => Rate {#319 ▶}
]

}

【问题讨论】:

  • 你的控制器方法中$this-&gt;rate-&gt;all()的输出是什么?您正在生成文本字段。你确定它们是正确生成的吗?此外,它们是否在正确提交表单后正确保存在您的数据库中?
  • $this-&gt;rate-&gt;all() 的结果是我在上面复制的集合。我很确定这些字段正在正确生成。

标签: php laravel laravel-5


【解决方案1】:

如果使用Form::select(),则第三个参数为默认值。

Form::select($field_name, $options, $selected);

您目前正在传递null。您需要传入一个或多个您希望在加载时选择的值。当您使用多选时,您需要传递一个 id 数组。

然后,重新填充该字段的最佳方法是使用 old() 辅助函数或更长的形式 Input::old()。例如

Form::select('myfield[]', $options, old('myfield', $default_if_no_old));

也就是说,您构建表单的方式意味着您不能使用这些方法,因为它们只支持字段名称的单一维度。

Form::select('supervisors[][supervisor_id]' ...

第二个维度,supervisor_id 导致生成的表单数据如下所示

[supervisors] => Array
(
    [0] => Array
        (
            [supervisor_id] => 2
        )

    [1] => Array
        (
            [supervisor_id] => 3
        )
)

当它重建表单时,它会检查key == [supervisor_id] =&gt; 2],它显然没有,所以它不会重新选择它。

理想地构建您的表单,只收集supervisor_ids[] 而不是supervisors[],并在幕后构建supervisors 数组。

如果你不能这样做,那么你需要构建一个自定义方法,也许是一个 Form::macro 来为你管理这个。

【讨论】:

  • 对不起,我错误地发布了错误的字段。我现在用正确的字段更新了我的问题,这些字段没有保留值,并且他们在刀片中使用 select() 方法。
猜你喜欢
  • 2018-11-01
  • 1970-01-01
  • 2017-12-28
  • 2012-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多