doucheng1891 2016-12-16 14: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 14: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!

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

报告相同问题?

悬赏问题

  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?