Someone Someone - 1 year ago 93
HTTP Question

How to pass input value to Request validation class in Laravel 5.2?

I have one controller and one Form Request Validation class:

. In
I use
storeItem(ItemValidation $request)
method to store item data which comes from

And in
request class I redirect back to the form with following method if validation fails.

public function response(array $errors){
return Redirect::back()->withErrors($errors)->withInput()->with('type', '2');

The main thing I want is to pass value from
(which is entered by user in form) to
method. I tried with following but it did not work:

public function response(array $errors, Request $request){
return Redirect::back()->withErrors($errors)->withInput()->with('type', '2')->with('sub_menu_id',$request->sub_menu_id);

Answer Source

->withInput() flashes the previous input to the session.

In your form view, you should use the old() helper to repopulate the value if a previous input exists.


<input type="text" name="username" value="{{ old('username') }}">

Plain PHP:

<input type="text" name="username" value="<?= e(old('username')) ?>">

If there is no previous input, it returns null, which echoes as an empty string.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download