RestPasswdValidator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | <?php namespace App\Service; use Illuminate\Support\Facades\Validator; use App\Service\BaseValidator; class RestPasswdValidator extends BaseValidator{ public $validatorsMessage; public $rules; public $message; protected $data; public $validator; public function __construct() { call_user_func_array([$ this , 'validator_rules' ],[]); } public function validator_rules(){ //验证规则 $ this ->rules = [ 'mobile' => 'required' , 'password' => 'required|min:6|max:16' , 'code' => 'required' , ]; $ this ->message = LangConfig( 'AuthMobileLoginValidator' ); } /** * 处理返回的data字段 * @return mixed */ public function setFields(){ array_walk_recursive($ this ->data,function(&$val,$key){ $val = trim($val); }); } /** * 验证码器钩子 */ public function hookAfter(){ if (!preg_match_all( "/^1[3456789]\d{9}$/" , trim(request()->input( 'mobile' )))) { $ this ->hookList = [ 'filed' => 'mobile' , 'message' =>LangConfig( 'MobileFormatError' )]; } } } |
1 2 3 4 5 6 7 | 'AuthMobileLoginValidator' =>[ 'mobile.required' => '手机号必填' , 'password.required' => '密码必填' , 'password.min' => '密码最少6位' , 'password.max' => '密码最多16位' , 'code.required' => '短信验证码必填' , ], |
BaseValidator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | <?php namespace App\Service; use Illuminate\Support\Facades\Validator; abstract class BaseValidator { public $validatorsMessage; public $rules; public $message; protected $data; public $validator; public $hookList = []; /* * 实现validator_rules * rule规则 */ abstract public function validator_rules(); /** * 处理返回的data字段 * @return mixed */ abstract public function setFields(); /** * @param $requestData * 验证数据合法性 */ public function make($requestData){ $ this ->data = $requestData; $ this ->validator = Validator::make($requestData, $ this ->rules,$ this ->message); $ this ->hook(); } public function hook(){ call_user_func_array([$ this , 'hookAfter' ],[]); $ this ->validator->after(function($validator){ if (!empty($ this ->hookList)){ $validator->errors()->add($ this ->hookList[ 'filed' ],$ this ->hookList[ 'message' ]); } }); } /** * 获取验证消息 * @return bool|mixed */ public function getMessage(){ if ($ this ->validator->fails() > 0){ $messages = $ this ->validator->errors(); return [0,$messages->first()]; } else { return [1, 'ok' ]; } } /* *返回处理过后的数据 */ public function getData(){ call_user_func_array([$ this , 'setFields' ],[]); return $ this ->data; } } |
控制器中使用
1 2 3 4 5 6 7 8 | //验证数据合法性 $rule = new RestPasswdValidator(); $rule->make($request->only( "mobile" , "password" , "code" )); list($err_code,$err_smg) = $rule->getMessage(); if (!$err_code){ throw new UserApiException($err_smg,501); } $data = $rule->getData(); |
本文来自博客园,作者:孙龙-程序员,转载请注明原文链接:https://www.cnblogs.com/sunlong88/articles/11102694.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能