doucheng1891 2016-12-16 06:11
浏览 62
已采纳

当验证失败时,Laravel 5.3表示不保留旧输入

I have a form where users can create an item. However, when the form is submitted and does not pass validation the old inputs are not being remembered and are simply wiped which is frustrating for the user.

I am using the laravelcollective forms for this and my html looks like:

 {{ Form::open(['route' => 'my.route', 'class' => 'form-horizontal', 'files' => true]) }}

<div class="form-group">
    {{ Form::label('name', 'Name', ['class' => 'control-label col-md-3']) }}
    <div class="col-md-9">
        {{ Form::text('name', null, ['placeholder' => 'hello world' ,'class' => 'form-control']) }}
    </div>
</div>

<div class="form-group">
    {{ Form::label('description', 'Description', ['class' => 'control-label col-md-3']) }}
    <div class="col-md-9">
        {{ Form::textarea('description', null, ['placeholder' => 'hello world', 'class' => 'form-control']) }}
        <span>some sub-text</span>
    </div>
</div>

<div class="form-group">
    {{ Form::label('date', 'Date', ['class' => 'control-label col-md-3']) }}
    <div class="col-md-9">
        {{ Form::text('date', null, ['placeholder' => 'hello world', 'class' => 'form-control']) }}
    </div>
</div>

{{ Form::close() }}

Even when I put the old value in like this it does not retain the old input

  {{ Form::text('name', old('name') , ['placeholder' => 'hello world' ,'class' => 'form-control']) }}

My method which stores the item in the back-end looks like this where ItemRequest takes care of validation.

public function store(ItemRequest $request, ImageMagick $imageMagick)
{
    $item = new Item;
    $item->name = $request->name;
    $item->description = $request->description;
    $item->date = $request->date;

    $item->save();
    return redirect()->route('some.other.route');
}

Trying to determine why old inputs are not being remembered.

展开全部

  • 写回答

1条回答 默认 最新

  • dongre6270 2016-12-16 06:12
    关注

    Use withInput() method at the end of redirect route method, like this:

    // If validation failed, use this method to make the old inputs available in the view
    return redirect()->route('some.other.route')->withInput();
    

    See more about Old Inputs in Laravel

    Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部