Someone
Someone

Reputation: 736

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

I have one controller and one Form Request Validation class: app\Http\Controllers\GuestController.php and app\Http\Requests\ItemValidation.php. In GuestController.php I use storeItem(ItemValidation $request) method to store item data which comes from <form> .

And in ItemValidation.php 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 <form> (which is entered by user in form) to response 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);
    }

Upvotes: 0

Views: 674

Answers (1)

Gras Double
Gras Double

Reputation: 16383

->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.

Blade:

<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.


Documentation: https://laravel.com/docs/5.3/requests#old-input

Upvotes: 1

Related Questions