Validation Laravel表单请求更改回发时的所有隐藏输入
我在一页上有以下两种表格:Validation Laravel表单请求更改回发时的所有隐藏输入,validation,laravel-5.1,Validation,Laravel 5.1,我在一页上有以下两种表格: {!! Form::model($user, ['method' => 'PATCH', 'action' => ['AccountController@update']]) !!} {!! Form::hidden('action', 'personal-details') !!} <div class="form-group"> {!! Form::label('name', 'Name', ['class
{!! Form::model($user, ['method' => 'PATCH', 'action' => ['AccountController@update']]) !!}
{!! Form::hidden('action', 'personal-details') !!}
<div class="form-group">
{!! Form::label('name', 'Name', ['class' => 'h4']) !!}
{!! Form::text('name', null, ['class' => 'form-control']) !!}
{!! errors_for('name', $errors) !!}
</div>
<div>
<button type="submit" class="btn btn-primary">Update personal details</button>
</div >
{!! Form::close() !!}
{!! Form::model($user, ['method' => 'PATCH', 'action' => ['AccountController@update']]) !!}
{!! Form::hidden('action', 'email') !!}
<div class="form-group">
{!! Form::label('new_email', 'New email address') !!}
{!! Form::email('new_email', Input::old('new_email'), ['class' => 'form-control']) !!}
{!! errors_for('new_email', $errors) !!}
</div>
<div>
<button type="submit" class="btn btn-primary">Update email address</button>
</div >
{!! Form::close() !!}
如果存在验证错误,在回发时,两个表单上的隐藏字段都将设置为原始已发布表单上的隐藏字段值。例如,若我提交个人详细信息表单,在回发时,两个表单上的操作字段的值都被设置为
个人详细信息
。如果我提交电子邮件表单,在发回后,两个表单上的隐藏字段都被设置为email
。为什么会发生这种情况?我如何修复它,以便回发时隐藏字段值不会更改?通过在响应方法的开头添加以下内容来修复它:
$this->dontFlash[]='action'代码>
class AccountRequest extends Request
{
protected $action;
public function authorize()
{
return true;
}
public function rules()
{
$rules = [];
$this->action = $this->input('action');
if ($this->action == 'personal-details') {
$rules['name'] = 'required|max:255';
}
if ($this->action == 'email') {
$rules['new_email'] = 'required|confirmed|email|max:255|unique:users,email';
}
return $rules;
}
public function response(array $errors)
{
if ($this->ajax() || $this->wantsJson()) {
return new JsonResponse($errors, 422);
}
return $this->redirector->to($this->getRedirectUrl() . '#'. $this->action)
->withInput($this->except($this->dontFlash))
->withErrors($errors, $this->errorBag);
}
}