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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失