温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Laravel 验证器如何实现多验证场景

发布时间:2021-03-10 16:25:47 来源:亿速云 阅读:202 作者:TREX 栏目:开发技术

这篇文章主要介绍“Laravel 验证器如何实现多验证场景”,在日常操作中,相信很多人在Laravel 验证器如何实现多验证场景问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Laravel 验证器如何实现多验证场景”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

Laravel 是什么

Laravel 是一套简洁、优雅的PHP Web开发框架。它可以让你从面条一样杂乱的代码中解脱出来;它可以帮你构建一个完美的网络APP,而且每行代码都可以简洁、富于表达力。


前言

在我们使用 laravel 框架的验证器,有的时候需要对表单等进行数据验证,当然 laravel 也为我们提供了
Illuminate\Http\Request 对象提供的 validate 方法 以及 FormRequest 和 Validator。

FormRequest 通过新建文件将我们的验证部分单独分开,来避免控制器臃肿。如果验证失败,就会生成一个让用户返回到先前的位置的重定向响应。这些错误也会被闪存到 Session 中,以便这些错误都可以在页面中显示出来。如果传入的请求是 AJAX,会向用户返回具有 422 状态代码和验证错误信息的 JSON 数据的 HTTP 响应。如果是接口请求或 ajax, 那么我们可能还需要将返回的 json 数据修改成我们想要的格式。

当我们实际开发中,可能一个模块需要有多个验证场景,如果为每一个验证场景都新建一个 FormRequest 不就太过繁琐了。
那么给 laravel 加上一个验证场景通过一个验证类一个模块或多个模块来适应不同的场景不就方便很多了。

开始

首先 我们封装了一个基类 BaseValidate.php 并将其放在 app\Validate 下,当然你也可以放在其他地方,只要修改好命名空间就好。

<?php namespace App\Validate; use Illuminate\Support\Facades\Validator; /**  * 扩展验证器  */ class BaseValidate {   /**    * 当前验证规则    * @var array    */   protected $rule = [];   /**    * 验证提示信息    * @var array    */   protected $message = [];   /**    * 验证场景定义    * @var array    */   protected $scene = [];   /**    * 设置当前验证场景    * @var array    */   protected $currentScene = null;   /**    * 验证失败错误信息    * @var array    */   protected $error = [];   /**    * 场景需要验证的规则    * @var array    */   protected $only = [];   /**    * 设置验证场景    * @access public    * @param string $name 场景名    * @return $this    */   public function scene($name)   {     // 设置当前场景     $this->currentScene = $name;     return $this;   }   /**    * 数据验证    * @access public    * @param array   $data 数据    * @param mixed   $rules 验证规则    * @param array  $message 自定义验证信息    * @param string  $scene 验证场景    * @return bool    */   public function check($data, $rules = [], $message = [],$scene = '')   {     $this->error =[];     if (empty($rules)) {       //读取验证规则       $rules = $this->rule;     }     if (empty($message)) {       $message = $this->message;     }     //读取场景     if (!$this->getScene($scene)) {       return false;     }     //如果场景需要验证的规则不为空     if (!empty($this->only)) {       $new_rules = [];       foreach ($this->only as $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();       return false;     }     return !empty($this->error) ? false : true;   }   /**    * 获取数据验证的场景    * @access protected    * @param string $scene 验证场景    * @return void    */   protected function getScene($scene = '')   {     if (empty($scene)) {       // 读取指定场景       $scene = $this->currentScene;     }     $this->only = [];     if (empty($scene)) {       return true;     }     if (!isset($this->scene[$scene])) {       //指定场景未找到写入error       $this->error = "scene:".$scene.'is not found';       return false;     }     // 如果设置了验证适用场景     $scene = $this->scene[$scene];     if (is_string($scene)) {       $scene = explode(',', $scene);     }     //将场景需要验证的字段填充入only     $this->only = $scene;     return true;   }   // 获取错误信息   public function getError()   {     return $this->error;   }  }

使用

接下来我们来验证一个文章的提交信息,首先我们新建一个文章验证类 ArticleValidate.php 并填充一些内容

<?php namespace App\Validate; use App\Validate\BaseValidate; /**  * 文章验证器  */ class ArticleValidate extends BaseValidate {   //验证规则   protected $rule =[     'id'=>'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

非场景验证

我们只需要定义好规则

public function update(){     $ArticleValidate = new ArticleValidate;     $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'],   ];

然后在我们的控制器进行数据验证

public function add(){     $ArticleValidate = new ArticleValidate;     $request_data = [       'title'=>'我是文章的标题',       'content'=>'我是文章的内容',     ];     if (!$ArticleValidate->scene('add')->check($request_data)) {       var_dump($ArticleValidate->getError());     }   }

控制器内验证

当然我们也允许你不创建验证类来验证数据,

public function add(){     $Validate = new BaseValidate;     $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 验证器如何实现多验证场景”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI