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

Install

composer require itwmw/engine-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
  • Rangine:Itwmw\Validate\Middleware\Rangine\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);