Blogger Information
Blog 94
fans 0
comment 0
visits 92098
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
验证器和静态代理
可乐随笔
Original
1256 people have browsed it

1.创建User验证器(继承validate验证类);
2.创建User验证器的静态代理类;
3.验证方法调用:验证器验证和独立验证;

User验证器

  1. <?php
  2. namespace app\validate;
  3. use think\Validate;
  4. //User验证器
  5. class User extends Validate
  6. {
  7. protected $rule = [
  8. 'name'=>[
  9. 'require',
  10. 'min'=>5,
  11. 'max'=>10,
  12. ],
  13. 'email'=>[
  14. 'require',
  15. 'email'=>'email',
  16. ],
  17. 'password'=>[
  18. 'require',
  19. 'min'=>6,
  20. 'max'=>20,
  21. ],
  22. 'mobile'=>[
  23. 'require',
  24. 'mobile'=>'mobile',
  25. ]
  26. ];
  27. }

User验证器的静态代理类

  1. <?php
  2. namespace app\facade;
  3. use think\Facade;
  4. class User extends Facade
  5. {
  6. protected static function getFacadeClass()
  7. {
  8. return '\app\validate\User';
  9. }
  10. }

验证方法调用

  1. <?php
  2. namespace app\index\controller;
  3. /*
  4. * 验证器总结
  5. * 1.验证器是一个自定义的类,必须继承于框架的验证类think\validate.php
  6. * 2.验证器可以创建在应用app目录下的任何一个可以访问的目录下面,这个访问是指控制器可以访问,并不是外部的URL访问,只需要指定正确的命名空间
  7. * 3.验证器就是完成框架的think\validate类的属性protected $rule=[]初始化
  8. * 4.在控制器中直接实例化调用 check()完成验证
  9. * 5.还可以创建一个自定义的静态代理,来统一验证方法的调用方式
  10. */
  11. use think\Controller;
  12. //use app\validate\User;
  13. //引入User验证器的静态代理
  14. use app\facade\User;
  15. class Demo9 extends Controller
  16. {
  17. //一、验证器验证
  18. //方法1:调用验证器类,User是validate的实现。
  19. public function test1()
  20. {
  21. //要验证的数据
  22. $data = [
  23. 'name' => 'mahairen',
  24. 'email' => 'nx99@qq.com',
  25. 'password' => '123abc',
  26. 'mobile' => '13909511163',
  27. ];
  28. $validate = new \app\validate\User();
  29. //调用验证器的check方法来验证数据
  30. if (!$validate->check($data)){
  31. return $validate->getError();
  32. }
  33. return '验证通过';
  34. }
  35. //方法2:使用静态代理User验证器,不用实例化了
  36. public function test2()
  37. {
  38. //要验证的数据
  39. $data = [
  40. 'name' => 'mahairen',
  41. 'email' => 'nx99@qq.com',
  42. 'password' => '123abc',
  43. 'mobile' => '13909511163',
  44. ];
  45. if (!User::check($data)) {
  46. return User::getError();
  47. }
  48. return '验证通过';
  49. }
  50. //方法3:(最常用)调用控制器中的validate方法进行验证:使用用户自定义的验证器/类
  51. public function test3()
  52. {
  53. //要验证的数据
  54. $data = [
  55. 'name' => 'mahairen',
  56. 'email' => 'nx99@qq.com',
  57. 'password' => '123abc',
  58. 'mobile' => '13909511163',
  59. ];
  60. //验证规则
  61. $validate = 'app\validate\User';
  62. $res = $this->validate($data,$validate);
  63. if (true !== $res){
  64. return $res;
  65. }
  66. return '验证通过';
  67. }
  68. //二.独立验证:使用validate类的rule属性
  69. //rule()方法实际上就是完成给当前类的protected $rule = []初始化
  70. public function test4()
  71. {
  72. //创建验证规则
  73. $rule = [
  74. 'name'=>[
  75. 'require',
  76. 'min'=>5,
  77. 'max'=>10,
  78. ],
  79. 'email'=>[
  80. 'require',
  81. 'email'=>'email',
  82. ],
  83. 'password'=>[
  84. 'require',
  85. 'min'=>6,
  86. 'max'=>20,
  87. ],
  88. 'mobile'=>[
  89. 'require',
  90. 'mobile'=>'mobile',
  91. ]
  92. ];
  93. //要验证的数据
  94. $data = [
  95. 'name' => 'mahairen',
  96. 'email' => 'nx99@qq.com',
  97. 'password' => '123abc',
  98. 'mobile' => '13909511163',
  99. ];
  100. //添加字段的验证规则,初始化rule属性
  101. validate::rule($rule);
  102. //校验数据,如果验证不通过,直接输出错误信息
  103. if (!validate::check($data)){
  104. return validate::getError();
  105. }
  106. return '验证通过';
  107. }
  108. }
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!