Laravel 5.2验证错误未出现
我正在尝试获取验证错误以显示在Laravel中.
I am trying to get validation errors to show up in Laravel.
我有一个类似的UserController设置:
I have a UserController set up like so:
<?php
namespace App\Http\Controllers;
use App\User;
use App\Http\Controllers\Controller;
//Use Request;
Use Flash;
Use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Show the profile for the given user.
*
* @param int $id
* @return Response
*/
public function showProfile($id)
{
return view('user.profile', ['user' => User::findOrFail($id)]);
}
public function store(Request $request) {
$this->validate($request, [
'email' => 'required|unique:users|email|max:255',
]);
if($this) {
$input = Request::all();
User::create($input);
return redirect('/');
}
else {
return redirect('/')->withErrors($validator);
}
}
}
在我看来(layout.blade.php),我包括:
In my view (layout.blade.php), I have included:
@if (count($errors) > 0)
@foreach ($errors->all() as $error)
{{!! $errors !!}}
@endforeach
@endif
要说明这条路线,我有:
To account for the route, I have:
Route::group(['middleware' => ['web']], function () {
Route::get('/', function (){
return view('home');
});
});
不幸的是,当我输入不应验证的坏"数据时,我没有看到任何错误(但是它没有存储在db中,所以就是这样).
Unfortunately, when I enter "bad" data that shouldn't be verified, I am not seeing any error (but it is not being stored in the db, so there's that).
另一个注意事项是,在呈现刀片模板时,我看到了一个额外的}"括号,但我不确定为什么会出现它.
One other note, when the blade template is rendered, I am seeing an extra "}" bracket, which I'm not sure why that is there.
有几处错误,或者可以在此处进行改进. UserController上的store方法有很多奇怪的问题. $this
将始终为true,因为php中的对象为true.另外,您将$validator
传递到withErrors
,这是没有意义的,因为没有变量validator
.
There are a couple things wrong or that can be improved here. The store method on the UserController has a lot of weird issues. $this
will always be true because objects are true in php. Also, you pass in $validator
into withErrors
which doesn't make sense because there's no variable validator
.
public function store(Request $request) {
$this->validate($request, [
'email' => 'required|unique:users|email|max:255',
]);
User::create(Request::all());
return redirect('/');
}
如果出现验证错误,validate
方法将抛出Illuminate\Foundation\Validation\ValidationException
.该异常应在App\Exceptions\Handler
的$dontReport
实例变量中列出,如下所示:
The validate
method will throw an Illuminate\Foundation\Validation\ValidationException
if there is a validation error. This exception should be listed in the $dontReport
instance variable in App\Exceptions\Handler
as seen below:
protected $dontReport = [
AuthorizationException::class,
HttpException::class,
ModelNotFoundException::class,
ValidationException::class,
];
如果您更改了这些值,删除或修改了ValidatesRequest
特征,则可能已破坏了此功能.
If you have changed these values, removed, or modified the ValidatesRequest
trait you may have broken this functionality.
您的错误报告代码也不正确:
Your error reporting code is not correct either:
@foreach ($errors->all() as $error)
{!! $errors->first() !!}
@endforeach
这里有3个变化.首先,我删除了外部错误大小检查,但这并没有给您带来任何好处.接下来,我修复了您额外的}
错误,取消转义数据的语法为{!! $errors->first() !!}
.最后,我调用了->first()
,它返回了与该特定字段相关的第一个错误.
There are 3 changes here. First I removed the outer errors size check, this doesn't really get you anything. Next, I fixed your extra }
error, the syntax for un-escaping data is {!! $errors->first() !!}
. Lastly, I called ->first()
this returns the first error associated with that particular field.
我认为必须注意,验证异常将创建对上一页的重定向响应.确定上一页的逻辑可以在Illuminate\Routing\UrlGenerator::previous()
中找到.
I think it's important to note that the validation exception will create a redirect response to the previous page. The logic for determining the previous page can be found in Illuminate\Routing\UrlGenerator::previous()
.