5

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.

Gerard Reches
  • 3,048
  • 3
  • 28
  • 39

2 Answers2

6
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')

itachi
  • 6,323
  • 3
  • 30
  • 40
1

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.

Jigar
  • 3,055
  • 1
  • 32
  • 51