【发布时间】: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->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 ▶}
]
}
【问题讨论】:
-
你的控制器方法中
$this->rate->all()的输出是什么?您正在生成文本字段。你确定它们是正确生成的吗?此外,它们是否在正确提交表单后正确保存在您的数据库中? -
$this->rate->all()的结果是我在上面复制的集合。我很确定这些字段正在正确生成。