我正在使用jQuery Chosen插件在Laravel中构建一个应用程序。当用户提交表单时,我正在使用请求来验证表单。但是,如果将它们重定向回来,例如,如果它们错过了必填字段,则选择的选择字段不会保留其值。
我的代码如下。我错过了什么?
控制器
/**
* 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->rate->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 ▶}
]
}
答案 0 :(得分:2)
如果您使用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] => 2]
是否显然没有,所以它不会重新选择它。
理想情况下,您可以构建表单并收集supervisor_ids[]
而不是supervisors[]
,并在幕后构建supervisors
数组。
如果你不能这样做,那么你需要构建一个自定义方法,可能是一个Form ::宏来管理它。