验证失败后输入中的 Laravel 重置值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34442817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel reset value in input after validation fail?
提问by Kieu Duy
I'm so confuse now, I'm learning Laravel from Laracast, according to the instructor, after validation fail, the form does not reset values that user entered. But when testing validation, when I submit the form reset every thing.
我现在很困惑,我正在从 Laracast 学习 Laravel,根据讲师的说法,验证失败后,表单不会重置用户输入的值。但是在测试验证时,当我提交表单时会重置所有内容。
Another question is the undefined variable$errors
when I try to access it.
另一个问题是当我尝试访问它时未定义的变量$errors
。
My Controller
我的控制器
<?php
namespace App\Http\Controllers;
use App\Articles;
use App\Http\Requests\CreateArticle;
use Carbon\Carbon;
use App\Http\Requests;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class ArticlesController extends Controller
{
public function create()
{
return view('articles.create');
}
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required',
'body' => 'required'
]);
Articles::create($request->all());
return redirect('articles');
}
}
My View
我的看法
@extends('app')
@section('content')
<h1>Create a new Articles</h1>
<hr/>
{!! Form::open(['url' => 'articles']) !!}
<div class="form-group">
{!! Form::label('title', 'Title: ') !!}
{!! Form::text('title', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('body', 'Body') !!}
{!! Form::textarea('body', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('published_at', 'Published On:') !!}
{!! Form::input('text', 'published_at', date('Y-m-d'), ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('submit', ['class' => 'btn btn-primary']) !!}
</div>
@if(isset($errors))
{{var_dump($errors)}}
@endif
{!! Form::close() !!}
@stop
He use v5.0 and I'm using v5.2
他使用 v5.0 而我使用 v5.2
回答by nrivero
return the following from your controller...
从您的控制器返回以下内容...
return redirect('articles')->withInput();
see if that helps. you can exclude certain fields if you wanted to like this..
看看是否有帮助。如果您想这样做,您可以排除某些字段。
return redirect('articles')->withInput($request->only('email'))
回答by user1669496
There are a couple of issues here.
这里有几个问题。
The first one, the inputs not being saved after validation fails. I believe this is because you are passing null
into the functions which are building the inputs when you should be passing in the value you wish it to default to. Try the following...
第一个,验证失败后未保存的输入。我相信这是因为null
当你应该传入你希望它默认的值时,你正在传入正在构建输入的函数。尝试以下...
<div class="form-group">
{!! Form::label('title', 'Title: ') !!}
{!! Form::text('title', old('title'), ['class' => 'form-control']) !!}
</div>
This should then populate the input element with the old data which was previously entered. Just follow the same procedure for the rest of the form inputs.
然后这应该用之前输入的旧数据填充输入元素。只需对其余的表单输入执行相同的过程。
The second issue with the $errors
value not being set is actually due to a change with Laravel 5.2. Many people have been having the same exact issue so I'd just like to refer you to a previous answer: Laravel 5.2 $errors not appearing in Blade
$errors
未设置值的第二个问题实际上是由于 Laravel 5.2 的更改。很多人都遇到了同样的问题,所以我只想向您推荐以前的答案:Laravel 5.2 $errors not seen in Blade
回答by Marco
If your are not using
如果您不使用
<div class="form-group">
{!! Form::label('title', 'Title: ') !!}
{!! Form::text('title', old('title'), ['class' => 'form-control']) !!}
</div>
but have simple input fields like
但有简单的输入字段,如
<input id="Email" name="Email" type="email" class="uit-input" placeholder="your email address" value={{old('Email')}}>
'prefill' the value with the old data.
用旧数据“预填充”该值。
回答by KEYDUC
Best way for you
最适合你的方式
<div class="form-group">
<label for="title">Title</label>
<input type="text" name="upload_title" class="form-control" value="{{ (old('title')) ? old('title') : $data->title}}">
</div>