2016-12-16 115 views
1

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

我现在用的是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() }} 

即使当我把旧的值在这样它不会保留旧的输入

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

我将项目存储在后端的方法看起来像这样,其中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'); 
} 

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

+0

您可以发布'ItemRequest'请 – Vuldo

回答

3

使用withInput()方法在重定向路由方法的结尾,就像这样:

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

查看更多有关Old Inputs in Laravel

希望这有助于!

+2

当验证因使用表单请求而失败时,不会调用此函数。 – Vuldo

+0

@Vuldo - 很好,我已经更新了我的答案!感谢:D –