0
所有隱藏輸入我有一個單頁上的以下2種形式:Laravel形式要求改變上回發
{!! Form::model($user, ['method' => 'PATCH', 'action' => ['[email protected]']]) !!}
{!! 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' => ['[email protected]']]) !!}
{!! 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() !!}
這是我的形式請求處理驗證:
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);
}
}
如果有驗證錯誤,在回發時,兩個表單上的隱藏字段將被設置爲隱藏字段值在原始發佈表單上的內容。例如。如果我提交個人詳細信息表單,在回發時,兩個表單上的操作字段的值將被設置爲personal-details
。如果我提交電子郵件表格,則在回發後,兩個表格上的隱藏字段將被設置爲email
。爲什麼會發生這種情況,我如何解決這個問題,以便回傳時隱藏的字段值不會改變?