I have the following 2 forms on a single page:
{!! 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() !!}
And this is my form request to handle validation:
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);
}
}
If there is a validation error, upon postback the hidden fields on both forms are being set to what the hidden field value was on the orginal posted form. E.g. if I submit the personal-details form, on postback, the value of the the action field on both forms is being set to personal-details
. If I submit the email form, upon post back the hidden field on both forms is being set to email
. Why is this happening and how can I fix it so that upon postback the hidden field value doesn't change?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire