Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep form values when redirect back in Laravel 4

I'm trying to keep the values of a form when Redirect::back on Laravel 4, but I can't find a way to do this.

This is my form:

{{ Form::open(array('route' => 'generate', 'files' => true)) }}

    {{ Form::radio('myType', '1', true); }}
    {{ Form::label('myType', '1'); }}

    {{ Form::radio('myType', '2'); }}
    {{ Form::label('myType', '2'); }}

    {{ Form::radio('myType', '3'); }}
    {{ Form::label('myType', '3'); }}

    {{ Form::text('myName'); }}

    {{ Form::file('uploadImage'); }}

    {{ Form::submit('Go'); }}

{{ Form::close() }}

And my controller:

$validator = Validator::make(Input::all(), array('uploadImage' => 'required|image', 'myName' => 'required'));

if ($validator->fails()){
    return Redirect::back()->withErrors($validator);
}

I tryed something like:

return Redirect::back()->withErrors($validator)->with('nameValue', Input::get('myName'));

And then in the view:

    {{ Form::text('myName', $nameValue); }}

But it still doesn't work. Any help will be appreciated.

like image 664
Gerard Reches Avatar asked Feb 28 '15 12:02

Gerard Reches


2 Answers

if ($validator->fails()){
    return Redirect::back()->withErrors($validator);
}

change to,

if ($validator->fails()){
    return Redirect::back()->withErrors($validator)->withInput();
}

you can retreive the value by Input::old() method.

read more

you tried: return Redirect::back()->withErrors($validator)->with('nameValue', Input::get('myName'));

above, you can get the value from Session.

Session::get('nameValue')

like image 169
itachi Avatar answered Nov 15 '22 22:11

itachi


Hey If you are using laravel 5.2 or greater version than you dont need to write

Redirect::back()

You can simply use back() helper function to do this.

so you can replace your code with below code.

if ($validator->fails()){
    return Redirect::back()->withErrors($validator)->withInput();
}

Here is a link for back() helper function documentation.

like image 39
Jigar Avatar answered Nov 15 '22 22:11

Jigar