Use attributes to specify the corresponding validator, scenario or validated field


composer require itwmw/engine-validate-attributes


use Itwmw\Validate\Attributes\Validator;

class UserController
    #[Validator(validate: UserValidate::class, scene: 'login')]
    public function login()

    #[Validator(validate: UserValidate::class, fields: ['user', 'pass'])]
    public function register()

Validator has three parameters which are:

  • $validate The full namespace of the validator
  • $scene Scene name, if this value is provided, the $fields parameter is invalid
  • $fields Fields to be validated, array type

Middleware Usage

If you use the middleware provided by the validator, you can register this extension to the middleware configuration at:

use Itwmw\Validate\Attributes\ValidateAttributesFactory;
use Itwmw\Validate\Support\Storage\ValidateMiddlewareConfig;

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

Next, the middleware will automatically get the validator specified by the annotation to complete the validation

Standalone use

The specified validator can be obtained by

$validate = (new \Itwmw\Validate\Attributes\ValidateAttributesFactory())->getValidate(UserController::class,"login");


The getValidate method takes two parameters

  • $controller Controller name or class name
  • $scene Method name or scene name, not required