Laravel中使用FormRequest进行表单验证方法及问题汇总
在`Laravel`中,每一个请求都会被封装为一个`Request`对象,`FormRequest`对象就是包含了额外验证逻辑(以及访问权限控制)的自定义`Request`类。本文分析了FormRequest异常的处理流程并提出了自定义处理FormRequest验证失败的思路。
所有示例基于Laravel5.1.39(LTS)
今天天气不错,我们来说说表单验证。
Controller中做表单验证
有的同学把表单验证逻辑写在Controller中,例如这个对用户提交评论内容的验证:
<?php
//...
useValidator;
classCommentController
{
publicfunctionpostStoreComment(Request$request)
{
$validator=Validator::make($request->all(),[
'comment'=>'required',//只是实例,就写个简单的规则,你的网站要是这么写欢迎在评论里贴网址
]);
if($validator->fails()){
returnredirect()
->back()
->withErrors($validator)
->withInput();
}
}
这样写的话,表单验证和业务逻辑挤在一起,我们的Controller中就会有太多的代码,而且重复的验证规则基本也是复制粘贴。
我们可以利用FormRequest来封装表单验证代码,从而精简Controller中的代码逻辑,使其专注于业务。而独立出去的表单验证逻辑甚至可以复用到其它请求中,例如修改评论。
什么是FormRequest
在Laravel中,每一个请求都会被封装为一个Request对象,FormRequest对象就是包含了额外验证逻辑(以及访问权限控制)的自定义Request类。
如何使用FormRequest做表单验证
Laravel提供了生成FormRequest的Artisan命令:
<code>$phpartisanmake:requestStoreCommentRequest</code>
于是就生成了app/Http/Requests/StoreCommentRequest.php,让我们来分析一下内容:
<?php
namespaceApp\Http\Requests;
useApp\Http\Requests\Request;//可以看到,这个基类是在我们的项目中的,这意味着我们可以修改它
classStoreCommentRequestextendsRequest
{
/**
*Determineiftheuserisauthorizedtomakethisrequest.
*
*@returnbool
*/
publicfunctionauthorize()//这个方法可以用来控制访问权限,例如禁止未付费用户评论…
{
returnfalse;//注意!这里默认是false,记得改成true
}
/**
*Getthevalidationrulesthatapplytotherequest.
*
*@returnarray
*/
publicfunctionrules()//这个方法返回验证规则数组,也就是Validator的验证规则
{
return[
//
];
}
}
那么很容易,我们除了让authorize方法返回true之外,还得让rules方法返回我们的验证规则:
<?php
//...
publicfunctionrules()
{
return[
];
}
//...
接着修改我们的Controller:
<?php
//...
//之前:publicfunctionpostStoreComment(Request$request)
publicfunctionpostStoreComment(\App\Http\Requests\StoreCommentRequest$request)
{
//...
}
//...
这样Laravel便会自动调用StoreCommentRequest进行表单验证了。
异常处理
如果表单验证失败,Laravel会重定向到之前的页面,并且将错误写到Session中,如果是AJAX请求,则会返回一段HTTP状态为422的JSON数据,类似这样:
<code>{comment:["Thecommentfieldisrequired."]}</code>
这里就不细说提示信息怎么修改了,如果有人想看相关教程,可以留言。
我们主要来说说怎么定制错误处理。
通常来说,Laravel中的错误都是异常(Exception),我们都可以在app\Exceptions\handler.php中进行统一处理。FormRequest确实也抛出了一个Illuminate\Http\Exception\HttpResponseException异常,但这个异常是在路由逻辑中就被特殊处理了。
首先我们来看看FormRequest是如何被执行的:
Illuminate\Validation\ValidationServiceProvider:
<?php
namespaceIlluminate\Validation;
useIlluminate\Support\ServiceProvider;
useIlluminate\Contracts\Validation\ValidatesWhenResolved;
classValidationServiceProviderextendsServiceProvider
{
/**
*Registertheserviceprovider.
*
*@returnvoid
*/
publicfunctionregister()
{
$this->registerValidationResolverHook();//看我看我看我
$this->registerPresenceVerifier();
$this->registerValidationFactory();
}
/**
*Registerthe"ValidatesWhenResolved"containerhook.
*
*@returnvoid
*/
protectedfunctionregisterValidationResolverHook()//对,就是我
{
//这里可以看到对`ValidatesWhenResolved`的实现做了一个监听
$this->app->afterResolving(function(ValidatesWhenResolved$resolved){
$resolved->validate();//然后调用了它的`validate`方法进行验证
});
}
//...
你猜对了,FormRequest就实现了这个Illuminate\Contracts\Validation\ValidatesWhenResolved接口:
<?php
namespaceIlluminate\Foundation\Http;
useIlluminate\Http\Request;
useIlluminate\Http\Response;
useIlluminate\Http\JsonResponse;
useIlluminate\Routing\Redirector;
useIlluminate\Container\Container;
useIlluminate\Contracts\Validation\Validator;
useIlluminate\Http\Exception\HttpResponseException;
useIlluminate\Validation\ValidatesWhenResolvedTrait;
useIlluminate\Contracts\Validation\ValidatesWhenResolved;//是你
useIlluminate\Contracts\Validation\FactoryasValidationFactory;
//我们`app\Http\Requests\Request`便是继承于这个`FormRequest`类
classFormRequestextendsRequestimplementsValidatesWhenResolved//就是你
{
useValidatesWhenResolvedTrait;//这个我们待会儿也要看看
//...
FormRequest基类中的validate方法是由这个Illuminate\Validation\ValidatesWhenResolvedTrait实现的:
Illuminate\Validation\ValidatesWhenResolvedTrait:
<?php
namespaceIlluminate\Validation;
useIlluminate\Contracts\Validation\ValidationException;
useIlluminate\Contracts\Validation\UnauthorizedException;
/**
*ProvidesdefaultimplementationofValidatesWhenResolvedcontract.
*/
traitValidatesWhenResolvedTrait
{
/**
*Validatetheclassinstance.
*
*@returnvoid
*/
publicfunctionvalidate()//这里实现了`validate`方法
{
$instance=$this->getValidatorInstance();//这里获取了`Validator`实例
if(!$this->passesAuthorization()){
$this->failedAuthorization();//这是调用了访问授权的失败处理
}elseif(!$instance->passes()){
$this->failedValidation($instance);//这里调用了验证失败的处理,我们主要看这里
}
}
//...
在validate里,如果验证失败了就会调用$this->failedValidation(),继续:
Illuminate\Foundation\Http\FormRequest:
<?php
//...
/**
*Handleafailedvalidationattempt.
*
*@param\Illuminate\Contracts\Validation\Validator$validator
*@returnmixed
*/
protectedfunctionfailedValidation(Validator$validator)
{
thrownewHttpResponseException($this->response(//这里抛出了传说中的异常
$this->formatErrors($validator)
));
}
终于看到异常了!可是这个异常在另一个地方被处理了:
Illuminate\Routing\Route:
<?php
//...
/**
*Runtherouteactionandreturntheresponse.
*
*@param\Illuminate\Http\Request$request
*@returnmixed
*/
publicfunctionrun(Request$request)
{
$this->container=$this->container?:newContainer;
try{
if(!is_string($this->action['uses'])){
return$this->runCallable($request);
}
if($this->customDispatcherIsBound()){
return$this->runWithCustomDispatcher($request);
}
return$this->runController($request);
}catch(HttpResponseException$e){//就是这里
return$e->getResponse();//这里直接返回了Response给客户端
}
}
//...
至此,整个思路已然清晰,不过我们还是看看这里生成的HttpResponseException异常中的Response是怎么生成的:
Illuminate\Foundation\Http\FormRequest:
<?php
//...
//132行:
if($this->ajax()||$this->wantsJson()){//对AJAX请求的处理
returnnewJsonResponse($errors,422);
}
return$this->redirector->to($this->getRedirectUrl())//对普通表单提交的处理
->withInput($this->except($this->dontFlash))
->withErrors($errors,$this->errorBag);
//...
相信你都看明白了。
如何实现自定义错误处理,这里提供两个思路,都需要重写app\Http\Requests\Request的failedValidation:
抛出一个新异常,继承HttpResponseException异常,重新实现getResponse方法,这个异常类我们可以放到app/Exceptions/下便于管理,错误返回依然交给Laravel;
抛出一个我们自定义的异常,在app\Exceptions\handler中处理。
具体实现这里就不写啦(参阅Laravel文档中关于错误处理部分,中文文档传送门),如果你有别的方法或者想法可以在评论中和我交流。
补充
如果你的Controller使用Illuminate\Foundation\Validation\ValidatesRequests这个Trait的validate方法进行验证,同样的,这里验证失败也会抛出Illuminate\Http\Exception\HttpResponseException异常,可以参考上面的解决方案进行处理。