Micro Dream Form ValidateMicro Dream Form Validate
Home
Plugin
Video Tutorial
Source Code
  • 6.x
  • 5.x
  • 简体中文
  • English
Home
Plugin
Video Tutorial
Source Code
  • 6.x
  • 5.x
  • 简体中文
  • English
  • Introduction
  • Plugin

    • Validator IDE Helper
    • Validator Routing Middleware

In addition to instantiating the validator directly and check, we can also use routing middleware to accomplish automatic validation

Install

composer require itwmw/validate-middleware

Using

Automatic validation can be done by introducing the corresponding validator middleware in the route

Middleware namespace:

  • Laravel:Itwmw\Validate\Middleware\Laravel\ValidateMiddleware
  • ThinkPHP:Itwmw\Validate\Middleware\Think\ValidateMiddleware

Other frameworks can be written based on the middleware logic that has been provided

In order for the middleware to do validation automatically, you need to call the static methods of Itwmw\Validate\Middleware\ValidateMiddlewareConfig to set auto-load validator rules and to set custom rule namespace prefixes.

/**
 * Set up auto-load validator rules
 * @param string $controllerPath Controller path
 * @param string $validatePath   Validator path
 * @return $this
 */
setAutoValidatePath(string $controllerPath, string $validatePath)

/**
 * Set the custom rules namespace prefix, If more than one exists, they all take effect
 * @param string $rulesPath Custom rules namespace prefixes
 * @return $this
 */
setRulesPath(string $rulesPath)

Example:

ValidateMiddlewareConfig::instance()->setAutoValidatePath('Itwmw\\App\\Controller\\', 'Itwmw\\App\\Model\\Validate\\');

If you need to specify a validator for a controller, or a validator for a method under a controller, or a validation scenario under a validator, you can use the setValidateLink method.

/**
 * Set Validator Association
 *
 * @param string|string[] $controller Controller namespace
 *                                    To specify a method, pass an array with the second element being the method name
 * @param string|string[] $validate   Validator namespace
 *                                    To specify a scene, pass an array with the second element being the scene name
 * @return $this
 */
setValidateLink($controller, $validate)
  • If the controller does not pass a method name and the validator passes a scene name, all the methods under that controller are using the specified scene under the specified validator
  • If the controller passes the method name and the validator does not pass the scenario name, the scenario is the default rule under the use of the specified validator
  • If neither the controller nor the validator specifies the second parameter, then only the validator is specified for the corresponding controller and the scenario will follow the default rules
  • If both the controller and the validation specify a second parameter, the method specified by the controller is validated using the scenario name specified by the validator

It is recommended to define the validator settings in Provider.

Take value

The get_validate_data method is used to retrieve the validated value, and the value retrieved is the validator collection type

$data = get_validate_data($request);

Dependency Injection

If you are using Laravel or ThinkPHP >= 8, you can directly use the RequestData class to obtain the validated values:

use Itwmw\Validate\Middleware\RequestData;

class DemoController
{
    public function index(RequestData $data) 
    {
        print_r($data);
    }
}

Defining Validators Using Annotations

In addition to configuring automatic loading of validator rules using setAutoValidatePath, validators can also be defined using annotations:

#[ValidatorAttribute(AuthValidator::class, 'login')]
public function login(Request $request): JsonResponse
{
    $data = get_validate_data($request);
}

Before using this approach, you need to configure the annotation validation factory:

use Itwmw\Validate\Middleware\ValidateMiddlewareConfig;
use Itwmw\Validate\Middleware\ValidateAttributesFactory;

ValidateMiddlewareConfig::instance()->setValidateFactory(new ValidateAttributesFactory());

It is recommended to define such settings in a Provider.

Using this validation factory does not affect the validation methods defined by setAutoValidatePath, but annotations have higher priority than auto-loaded configurations.

Prev
Validator IDE Helper