为你的 Laravel 验证器加上多验证场景的实现
前言
在我们使用laravel框架的验证器,有的时候需要对表单等进行数据验证,当然laravel也为我们提供了
Illuminate\Http\Request对象提供的validate方法以及FormRequest和Validator。
FormRequest通过新建文件将我们的验证部分单独分开,来避免控制器臃肿。如果验证失败,就会生成一个让用户返回到先前的位置的重定向响应。这些错误也会被闪存到Session中,以便这些错误都可以在页面中显示出来。如果传入的请求是AJAX,会向用户返回具有422状态代码和验证错误信息的JSON数据的HTTP响应。如果是接口请求或ajax,那么我们可能还需要将返回的json数据修改成我们想要的格式。
当我们实际开发中,可能一个模块需要有多个验证场景,如果为每一个验证场景都新建一个FormRequest不就太过繁琐了。
那么给laravel加上一个验证场景通过一个验证类一个模块或多个模块来适应不同的场景不就方便很多了。
开始
首先我们封装了一个基类BaseValidate.php并将其放在app\Validate下,当然你也可以放在其他地方,只要修改好命名空间就好。
currentScene=$name;
return$this;
}
/**
*数据验证
*@accesspublic
*@paramarray$data数据
*@parammixed$rules验证规则
*@paramarray$message自定义验证信息
*@paramstring$scene验证场景
*@returnbool
*/
publicfunctioncheck($data,$rules=[],$message=[],$scene='')
{
$this->error=[];
if(empty($rules)){
//读取验证规则
$rules=$this->rule;
}
if(empty($message)){
$message=$this->message;
}
//读取场景
if(!$this->getScene($scene)){
returnfalse;
}
//如果场景需要验证的规则不为空
if(!empty($this->only)){
$new_rules=[];
foreach($this->onlyas$key=>$value){
if(array_key_exists($value,$rules)){
$new_rules[$value]=$rules[$value];
}
}
$rules=$new_rules;
}
//var_dump($rules);die;
$validator=Validator::make($data,$rules,$message);
//验证失败
if($validator->fails()){
$this->error=$validator->errors()->first();
returnfalse;
}
return!empty($this->error)?false:true;
}
/**
*获取数据验证的场景
*@accessprotected
*@paramstring$scene验证场景
*@returnvoid
*/
protectedfunctiongetScene($scene='')
{
if(empty($scene)){
//读取指定场景
$scene=$this->currentScene;
}
$this->only=[];
if(empty($scene)){
returntrue;
}
if(!isset($this->scene[$scene])){
//指定场景未找到写入error
$this->error="scene:".$scene.'isnotfound';
returnfalse;
}
//如果设置了验证适用场景
$scene=$this->scene[$scene];
if(is_string($scene)){
$scene=explode(',',$scene);
}
//将场景需要验证的字段填充入only
$this->only=$scene;
returntrue;
}
//获取错误信息
publicfunctiongetError()
{
return$this->error;
}
}
使用
接下来我们来验证一个文章的提交信息,首先我们新建一个文章验证类ArticleValidate.php并填充一些内容
'required', 'title'=>'required|max:255', 'content'=>'required', ]; //自定义验证信息 protected$message=[ 'id.required'=>'缺少文章id', 'title.required'=>'请输入title', 'title.max'=>'title长度不能大于255', 'content.required'=>'请输入内容', ]; //自定义场景 protected$scene=[ 'add'=>"title,content", 'edit'=>['id','title','content'], ]; }
如上所示,在这个类中我们定义了验证规则rule,自定义验证信息message,以及验证场景scene
非场景验证
我们只需要定义好规则
publicfunctionupdate(){
$ArticleValidate=newArticleValidate;
$request_data=[
'id'=>'1',
'title'=>'我是文章的标题',
'content'=>'我是文章的内容',
];
if(!$ArticleValidate->check($request_data)){
var_dump($ArticleValidate->getError());
}
}
check方法中总共有四个参数,第一个要验证的数据,第二个验证规则,第三个自定义错误信息,第四个验证场景,其中2,3,4非必传。
如果验证未通过我们调用getError()方法来输出错误信息,getError()暂不支持返回所有验证错误信息。
场景验证
我们需要提前在验证类中定义好验证场景
如下,支持使用字符串或数组,使用字符串时,要验证的字段需用,隔开
//自定义场景 protected$scene=[ 'add'=>"title,content", 'edit'=>['id','title','content'], ];
然后在我们的控制器进行数据验证
publicfunctionadd(){
$ArticleValidate=newArticleValidate;
$request_data=[
'title'=>'我是文章的标题',
'content'=>'我是文章的内容',
];
if(!$ArticleValidate->scene('add')->check($request_data)){
var_dump($ArticleValidate->getError());
}
}
控制器内验证
当然我们也允许你不创建验证类来验证数据,
publicfunctionadd(){
$Validate=newBaseValidate;
$request_data=[
'title'=>'我是文章的标题',
'content'=>'我是文章的内容',
];
$rule=[
'id'=>'required',
'title'=>'required|max:255',
'content'=>'required',
];
//自定义验证信息
$message=[
'id.required'=>'缺少文章id',
'title.required'=>'请输入title',
'title.max'=>'title长度不能大于255',
'content.required'=>'请输入内容',
];
if(!$Validate->check($request_data,$rule,$message)){
var_dump($Validate->getError());
}
}
通过验证场景,既减少了控制器代码的臃肿,又减少了FormRequest文件过多,还可以自定义json数据是不是方便多了呢,
参考文档
laravel表单验证:表单验证《Laravel5.5中文文档》
thinkphp验证场景:https://www.kancloud.cn/manual/thinkphp5_1/354104
到此这篇关于为你的Laravel验证器加上多验证场景的实现的文章就介绍到这了,更多相关Laravel验证器内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
本文为杨攀遥原创文章,如若转载,无需和我联系,但请注明出处[杨攀遥的博客]:https://www.yangpanyao.com/archives/120.html