hughjidette
hughjidette

Reputation: 136

Repopulating user inputs after successful validation

I am trying to create a search form where the user has to select from some dropdown menus and enter text in one of a few fields. The problem is I am redisplaying the search page with results below it. To do this I am not redirecting, I am just returning a view with the datasets I need compacted along with it.

Is there any way to get to retrieve input similar to how you would do this Input::old('x') when you were redirecting after failed validation?

The routes are:

Route::get('search', ['as' => 'main.search.get', 'uses' => 'MainController@showSearchPage']);

Route::post('search', ['as' => 'main.search.post', 'uses' => 'MainController@showSearchResults']); 

Example of code I have in the view:

 {!! Form::open(array('route' => 'main.search.post', 'class' => 'form-inline align-form-center', 'role' => 'form')) !!}

 <div class="form-group">
    {!! Form::label('product_code', 'Product Code: ',['class' => 'control-label  label-top']) !!}
    {!! Form::text('product_code', Input::old('product_code'), ['class' => 'form-control input-sm']) !!}
</div>

So when you submit a search, it calls showSearchResults which then returns a view if it succeeds, if it fails validation via my SearchRequest class it gets redirected to the main.search.get route, errors are printed and input is returned to the fields.

I have done a lot of searching and have come up more or less empty handed, it would be nice if there was a way to say ->withInput() when returning a view (not redirecting) or something.

Currently my only solution is to Input::flash() but since I am not redirecting that data persists for an extra refresh. That isn't a terribly big deal at this point, but I was wondering if anyone else had a better solution.

Edit - Code below from controller where view is returned:

...

   Input::flash();
   return view('main.search', compact('results', 'platformList', 'versionList', 'customerList', 'currencyList', 'customer', 'currency'));
}

Thank you

Upvotes: 2

Views: 856

Answers (2)

Hlaing Tin Htun
Hlaing Tin Htun

Reputation: 17

You can use request instead of old since its the post request

change {{old('product_code')}} to {{request('product_code')}}

Upvotes: 0

Aine
Aine

Reputation: 2708

I had the same problem. The solution that worked for me was to add the following line into the controller.

session(['_old_input' => $request->input()]);

Now I'll explain how it works.

In the view, the global function old() is called:

<input type="username" id="username" class="form-control" name="username" value="{{ old('username') }}" placeholder="Username" autofocus>

This function is in vendor/laravel/framework/src/Illuminate/Foundation/helpers.php

function old($key = null, $default = null)
{
    return app('request')->old($key, $default);
}

This calls Illuminate\Http\Request->old():

public function old($key = null, $default = null)
{
    return $this->session()->getOldInput($key, $default);
}

Which calls Illuminate\Session\Store->getOldInput():

public function getOldInput($key = null, $default = null)
{
    $input = $this->get('_old_input', []);
    return Arr::get($input, $key, $default);
}

This call is looking for _old_input in the session. So the solution is to add the input to the session using this value.

Hope this helps.

Upvotes: 1

Related Questions