欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

如何优雅的使用 laravel 的 validator验证方法

程序员文章站 2022-06-29 15:25:14
web 开发过程中经常会需要进行参数验证,laravel 中我们常用 validator 或者 request 这两种方法来进行验证,但是这两种验证都不是很方便进行自定义提...

web 开发过程中经常会需要进行参数验证,laravel 中我们常用 validator 或者 request 这两种方法来进行验证,但是这两种验证都不是很方便进行自定义提示信息,自定义验证规则,所以下面来介绍一种很方便的用法:

新建抽象类

<?php

namespace app\http\validators;

use validator;

abstract class abstractvalidator
{

 /**
  * validator
  *
  * @var \illuminate\validation\factory
  */
 protected $validator;

 /**
  * validation data key => value array
  *
  * @var array
  */
 protected $data = array();

 /**
  * validation errors
  *
  * @var array
  */
 protected $errors = array();

 /**
  * validation rules
  *
  * @var array
  */
 protected $rules = array();

 /**
  * validation messages
  *
  * @var array
  */
 protected $messages = array();

 /**
  * validation codes
  *
  * @var array
  */
 protected $codes = array();

 public function __construct(array $data)
 {
  $this->data = $data;
  $this->before();
  $this->validator = validator::make($this->data, $this->rules, $this->messages);
  $this->after();
 }

 /**
  * set data to validate
  *
  * @return validator
  */
 public function getvalidator()
 {
  return $this->validator;
 }

 /**
  * set data to validate
  *
  * @return $this
  */
 public function with(array $data)
 {
  $this->data = $data;
  $this->before();
  $this->validator = $this->validator->make($this->data, $this->rules, $this->messages);
  $this->after();
  return $this;
 }

 /**
  * validation passes or fails
  *
  * @return boolean
  */
 public function passes()
 {
  if ($this->validator->fails()) {
   $this->errors = $this->validator->messages();

   return false;
  }

  return true;
 }

 /**
  * return errors, if any
  *
  * @return array
  */
 public function errors()
 {
  return $this->errors;
 }

 /**
  * return errors codes, if any
  *
  * @return array
  */
 public function getcodes()
 {
  return $this->codes;
 }

 /**
  * getrules
  *
  * @return array
  */
 public function getrules()
 {
  return $this->rules;
 }

 /**
  * getdata
  *
  * @return array
  */
 public function getdata()
 {
  return $this->data;
 }

 /**
  * geterrors
  *
  * @return array
  */
 public function geterrors()
 {
  return $this->errors;
 }

 /**
  * getmessages
  *
  * @return array
  */
 public function getmessages()
 {
  return $this->messages;
 }

 /**
  * setrule
  *
  * @param string $key
  * @param string $value
  *
  * @return $this
  */
 public function setrule($key, $value)
 {
  $this->rules[$key] = $value;

  return $this;
 }

 /**
  * emptyrules
  *
  * @return $this
  */
 public function emptyrules()
 {
  $this->rules = array();

  return $this;
 }

 /**
  * sometimes
  *
  * @param string  $attribute
  * @param string|array $rules
  * @param callable  $callback
  *
  * @return $this
  */
 public function sometimes($attribute, $rules, callable $callback)
 {
  $this->validator->sometimes($attribute, $rules, $callback);

  return $this;
 }

 /**
  * resolver
  *
  * @param closure $resolver
  *
  * @return $this
  */
 public function resolver(closure $resolver)
 {
  validator::resolver($resolver);

  return $this;
 }

 /**
  * replacer
  *
  * @param closure $resolver
  *
  * @return $this
  */
 public function replacer($replace, closure $resolver)
 {
  validator::replacer($replace, $resolver);

  return $this;
 }

 /**
  * extendimplicit
  *
  * @param closure $resolver
  *
  * @return $this
  */
 public function extendimplicit($extendimplicit, closure $resolver)
 {
  validator::extendimplicit($extendimplicit, $resolver);

  return $this;
 }

 /**
  * extend
  *
  * @param string   $rule
  * @param \closure|string $extension
  * @param string   $message
  *
  * @return $this
  */
 public function extend($rule, $extension, $message = null)
 {
  validator::extend($rule, $extension, $message);

  return $this;
 }

 /**
  * before (extend(),resolver())
  *
  * @return $this
  */
 public function before()
 {
 }

 /**
  * after(sometimes())
  *
  * @return $this
  */
 public function after()
 {
 }
}

新建中间件

<?php

namespace app\http\middleware;

use closure;
use \illuminate\http\request;

class validateadminmiddleware
{
 /**
  * this namespace is applied to the controller routes in your routes file.
  *
  * in addition, it is set as the url generator's root namespace.
  *
  * @var string
  */
 protected $namespace = 'app\http\validators';

 /**
  * handle an incoming request.
  *
  * @param \illuminate\http\request $request
  * @param \closure     $next
  *
  * @return mixed
  */
 public function handle(request $request, closure $next, $validator = null)
 {
  if ($request->ismethod('post')) {
   $type = $request->segment(1);
   if ($validator) {
    $validator = $this->namespace . '\\' . studly_case($type) . '\\' . studly_case($validator) . 'validator';
    $validator = new $validator($request->all());

    if (!$validator->passes()) {
     if ($request->isajax()) {
      return $validator->errors()->first();
     } else {
      return redirect()->back()
      ->witherrors($validator->getvalidator())
      ->withinput();
     }
    }
   }
  }
  return $next($request);
 }
}

新建 testtestvalidator

<?php

namespace app\http\validators\admin;

use app\http\validators\abstractvalidator;

class testvalidator extends abstractvalidator
{
 /**
  * validation rules
  *
  * @var array
  */
 protected $rules = array(
  'name' => ['required', 'test', 'min:1'],
 );

 /**
  * validation messages
  *
  * @var array
  */
 protected $messages = array(
  'name.required' => '必填',
  'name.min' => '最少1个字符',
  'name.test' => '测试',
 );

 /**
  * 自定义验证规则或者扩展validator类
  */
 public function before()
 {
  $this->extend('test', function ($attribute, $value, $parameters) {
   return bool;
  });
 }
}

路由中如何使用

route::post('/', ['middleware' => ['valiadmin:test'], 'uses' => 'indexcontroller@test']);

具体使用可以自行配置~

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。