• 售前

  • 售后

热门帖子
入门百科

怎样优雅的利用 laravel 的 validator验证方法

[复制链接]
半岛宫殿rw 显示全部楼层 发表于 2021-10-26 12:40:40 |阅读模式 打印 上一主题 下一主题
web 开发过程中经常会必要举行参数验证,laravel 中我们常用 validator 大概 request 这两种方法来举行验证,但是这两种验证都不是很方便举行自定义提示信息,自定义验证规则,所以下面来先容一种很方便的用法:
新建抽象类
  1. <?php
  2. namespace App\Http\Validators;
  3. use Validator;
  4. abstract class AbstractValidator
  5. {
  6. /**
  7.   * Validator
  8.   *
  9.   * @var \Illuminate\Validation\Factory
  10.   */
  11. protected $validator;
  12. /**
  13.   * Validation data key => value array
  14.   *
  15.   * @var array
  16.   */
  17. protected $data = array();
  18. /**
  19.   * Validation errors
  20.   *
  21.   * @var array
  22.   */
  23. protected $errors = array();
  24. /**
  25.   * Validation rules
  26.   *
  27.   * @var array
  28.   */
  29. protected $rules = array();
  30. /**
  31.   * Validation messages
  32.   *
  33.   * @var array
  34.   */
  35. protected $messages = array();
  36. /**
  37.   * Validation codes
  38.   *
  39.   * @var array
  40.   */
  41. protected $codes = array();
  42. public function __construct(array $data)
  43. {
  44.   $this->data = $data;
  45.   $this->before();
  46.   $this->validator = Validator::make($this->data, $this->rules, $this->messages);
  47.   $this->after();
  48. }
  49. /**
  50.   * Set data to validate
  51.   *
  52.   * @return validator
  53.   */
  54. public function getValidator()
  55. {
  56.   return $this->validator;
  57. }
  58. /**
  59.   * Set data to validate
  60.   *
  61.   * @return $this
  62.   */
  63. public function with(array $data)
  64. {
  65.   $this->data = $data;
  66.   $this->before();
  67.   $this->validator = $this->validator->make($this->data, $this->rules, $this->messages);
  68.   $this->after();
  69.   return $this;
  70. }
  71. /**
  72.   * Validation passes or fails
  73.   *
  74.   * @return boolean
  75.   */
  76. public function passes()
  77. {
  78.   if ($this->validator->fails()) {
  79.    $this->errors = $this->validator->messages();
  80.    return false;
  81.   }
  82.   return true;
  83. }
  84. /**
  85.   * Return errors, if any
  86.   *
  87.   * @return array
  88.   */
  89. public function errors()
  90. {
  91.   return $this->errors;
  92. }
  93. /**
  94.   * Return errors codes, if any
  95.   *
  96.   * @return array
  97.   */
  98. public function getCodes()
  99. {
  100.   return $this->codes;
  101. }
  102. /**
  103.   * getRules
  104.   *
  105.   * @return array
  106.   */
  107. public function getRules()
  108. {
  109.   return $this->rules;
  110. }
  111. /**
  112.   * getData
  113.   *
  114.   * @return array
  115.   */
  116. public function getData()
  117. {
  118.   return $this->data;
  119. }
  120. /**
  121.   * getErrors
  122.   *
  123.   * @return array
  124.   */
  125. public function getErrors()
  126. {
  127.   return $this->errors;
  128. }
  129. /**
  130.   * getMessages
  131.   *
  132.   * @return array
  133.   */
  134. public function getMessages()
  135. {
  136.   return $this->messages;
  137. }
  138. /**
  139.   * setRule
  140.   *
  141.   * @param string $key
  142.   * @param string $value
  143.   *
  144.   * @return $this
  145.   */
  146. public function setRule($key, $value)
  147. {
  148.   $this->rules[$key] = $value;
  149.   return $this;
  150. }
  151. /**
  152.   * emptyRules
  153.   *
  154.   * @return $this
  155.   */
  156. public function emptyRules()
  157. {
  158.   $this->rules = array();
  159.   return $this;
  160. }
  161. /**
  162.   * sometimes
  163.   *
  164.   * @param string  $attribute
  165.   * @param string|array $rules
  166.   * @param callable  $callback
  167.   *
  168.   * @return $this
  169.   */
  170. public function sometimes($attribute, $rules, callable $callback)
  171. {
  172.   $this->validator->sometimes($attribute, $rules, $callback);
  173.   return $this;
  174. }
  175. /**
  176.   * resolver
  177.   *
  178.   * @param Closure $resolver
  179.   *
  180.   * @return $this
  181.   */
  182. public function resolver(Closure $resolver)
  183. {
  184.   Validator::resolver($resolver);
  185.   return $this;
  186. }
  187. /**
  188.   * replacer
  189.   *
  190.   * @param Closure $resolver
  191.   *
  192.   * @return $this
  193.   */
  194. public function replacer($replace, Closure $resolver)
  195. {
  196.   Validator::replacer($replace, $resolver);
  197.   return $this;
  198. }
  199. /**
  200.   * extendImplicit
  201.   *
  202.   * @param Closure $resolver
  203.   *
  204.   * @return $this
  205.   */
  206. public function extendImplicit($extendImplicit, Closure $resolver)
  207. {
  208.   Validator::extendImplicit($extendImplicit, $resolver);
  209.   return $this;
  210. }
  211. /**
  212.   * extend
  213.   *
  214.   * @param string   $rule
  215.   * @param \Closure|string $extension
  216.   * @param string   $message
  217.   *
  218.   * @return $this
  219.   */
  220. public function extend($rule, $extension, $message = null)
  221. {
  222.   Validator::extend($rule, $extension, $message);
  223.   return $this;
  224. }
  225. /**
  226.   * before (extend(),resolver())
  227.   *
  228.   * @return $this
  229.   */
  230. public function before()
  231. {
  232. }
  233. /**
  234.   * after(sometimes())
  235.   *
  236.   * @return $this
  237.   */
  238. public function after()
  239. {
  240. }
  241. }
复制代码
新建中心件
  1. <?php
  2. namespace App\Http\Middleware;
  3. use Closure;
  4. use \Illuminate\Http\Request;
  5. class ValidateAdminMiddleware
  6. {
  7. /**
  8.   * This namespace is applied to the controller routes in your routes file.
  9.   *
  10.   * In addition, it is set as the URL generator's root namespace.
  11.   *
  12.   * @var string
  13.   */
  14. protected $namespace = 'App\Http\Validators';
  15. /**
  16.   * Handle an incoming request.
  17.   *
  18.   * @param \Illuminate\Http\Request $request
  19.   * @param \Closure     $next
  20.   *
  21.   * @return mixed
  22.   */
  23. public function handle(Request $request, Closure $next, $validator = null)
  24. {
  25.   if ($request->isMethod('POST')) {
  26.    $type = $request->segment(1);
  27.    if ($validator) {
  28.     $validator = $this->namespace . '\\' . studly_case($type) . '\\' . studly_case($validator) . 'Validator';
  29.     $validator = new $validator($request->all());
  30.     if (!$validator->passes()) {
  31.      if ($request->isAjax()) {
  32.       return $validator->errors()->first();
  33.      } else {
  34.       return redirect()->back()
  35.       ->withErrors($validator->getValidator())
  36.       ->withInput();
  37.      }
  38.     }
  39.    }
  40.   }
  41.   return $next($request);
  42. }
  43. }
复制代码
新建 TestTestValidator
  1. <?php
  2. namespace App\Http\Validators\Admin;
  3. use App\Http\Validators\AbstractValidator;
  4. class TestValidator extends AbstractValidator
  5. {
  6. /**
  7.   * Validation rules
  8.   *
  9.   * @var Array
  10.   */
  11. protected $rules = array(
  12.   'name' => ['required', 'test', 'min:1'],
  13. );
  14. /**
  15.   * Validation messages
  16.   *
  17.   * @var Array
  18.   */
  19. protected $messages = array(
  20.   'name.required' => '必填',
  21.   'name.min' => '最少1个字符',
  22.   'name.test' => '测试',
  23. );
  24. /**
  25.   * 自定义验证规则或者扩展Validator类
  26.   */
  27. public function before()
  28. {
  29.   $this->extend('test', function ($attribute, $value, $parameters) {
  30.    return bool;
  31.   });
  32. }
  33. }
复制代码
路由中怎样利用
  1. Route::post('/', ['middleware' => ['valiAdmin:Test'], 'uses' => 'IndexController@test']);
复制代码
具体利用可以自行设置~
以上就是本文的全部内容,盼望对大家的学习有所资助,也盼望大家多多支持草根技术分享。

帖子地址: 

回复

使用道具 举报

分享
推广
火星云矿 | 预约S19Pro,享500抵1000!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

草根技术分享(草根吧)是全球知名中文IT技术交流平台,创建于2021年,包含原创博客、精品问答、职业培训、技术社区、资源下载等产品服务,提供原创、优质、完整内容的专业IT技术开发社区。
  • 官方手机版

  • 微信公众号

  • 商务合作