Keep form values when redirect back in Laravel 4

China☆狼群 提交于 2019-12-12 11:51:15

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/28781617/keep-form-values-when-redirect-back-in-laravel-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!