多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 17. 插件验证器 ## 插件验证器 验证器同TP的一致,更多的验证可参考 [https://www.kancloud.cn/manual/thinkphp6\_0/1037629](https://www.kancloud.cn/manual/thinkphp6_0/1037629) 文件路径:例如demo插件 addons/demo/validate/Index.php ![](https://img.kancloud.cn/a5/2c/a52cf2d20710e93ade6c172a1f03c8de_339x250.png) ## 示例 ``` <pre class="calibre16">``` <span class="token"><</span><span class="token">?</span>php declare <span class="token1">(</span>strict_types <span class="token">=</span> <span class="token3">1</span><span class="token1">)</span><span class="token1">;</span> namespace addons\demo\validate<span class="token1">;</span> use think\Validate<span class="token1">;</span> class <span class="token6">Index</span> extends <span class="token6">Validate</span> <span class="token1">{</span> <span class="token5">/** * 定义验证规则 * 格式:'字段名' => ['规则1','规则2'...] * @var array */</span> protected $rule <span class="token">=</span> <span class="token1">[</span> <span class="token4">'username'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'require|alphaDash|length:3,16|checkUser'</span><span class="token1">,</span> <span class="token1">]</span><span class="token1">;</span> <span class="token5">/** * 定义错误信息 * 格式:'字段名.规则名' => '错误信息' * @var array */</span> protected $message <span class="token">=</span> <span class="token1">[</span> <span class="token4">'username.checkUser'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'用户名已存在'</span><span class="token1">,</span> <span class="token1">]</span><span class="token1">;</span> <span class="token5">/** * 自定义方法 * @param $value * @param $rule * @param array $data * @return bool|string */</span> protected <span class="token2">function</span> <span class="token6">checkUser</span><span class="token1">(</span>$value<span class="token1">,</span> $rule<span class="token1">,</span> $data<span class="token">=</span><span class="token1">[</span><span class="token1">]</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span> <span class="token3">true</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span> ``` ``` 控制器使用 ``` <pre class="calibre20">``` <span class="token2">try</span> <span class="token1">{</span> $data <span class="token">=</span> <span class="token1">[</span><span class="token1">]</span><span class="token1">;</span> <span class="token6">validate</span><span class="token1">(</span>\addons\demo\validate\Index<span class="token1">:</span><span class="token1">:</span>class<span class="token1">)</span><span class="token">-</span><span class="token">></span><span class="token6">check</span><span class="token1">(</span>$data<span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token2">catch</span> <span class="token1">(</span><span class="token6">ValidateException</span> $e<span class="token1">)</span> <span class="token1">{</span> <span class="token5">// 验证失败 输出错误信息</span> <span class="token6">dump</span><span class="token1">(</span>$e<span class="token">-</span><span class="token">></span><span class="token6">getError</span><span class="token1">(</span><span class="token1">)</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> ``` ```