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

1 php forms laravel laravel-5 laravel-form

我有一个表单,用户可以在其中创建一个项目。然而,当表单被提交并且没有通过验证时,旧的输入不会被记住并且被简单地擦除,这对用户来说是令人沮丧的。

我为此使用了 laravelcollective 表单,我的 html 看起来像:

 {{ 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() }}
Run Code Online (Sandbox Code Playgroud)

即使我像这样输入旧值,它也不会保留旧输入

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

我在后端存储项目的方法看起来像这样,其中 ItemRequest 负责验证。

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');
}
Run Code Online (Sandbox Code Playgroud)

试图确定为什么旧的输入没有被记住。

Sau*_*ogi 5

withInput()在重定向路由方法末尾使用方法,如下所示:

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

查看有关Laravel 中旧输入的更多信息

希望这可以帮助!

  • 验证失败时不会调用此方法,因为他使用了表单请求。 (2认同)