Non-annotation validator

The annotation method refers to and uses the validator. There are restrictions. It can only be used in specific locations such as Http server/ Rpc server / Websocket server. In actual business development, parameter verification is also involved in other places. Both non-annotation and annotation methods refer to the same validator. A validator can be used in multiple locations and in multiple ways, greatly reducing the duplication cost of the code.

Global method

 function validate(array $data, string $validatorName, array $fields = [], array $userValidators = []): array 

Global function usage, throws a Swoft\Validator\Exception\ValidatorException when the validator fails

  • $data data to be validated, must be in array KV format
  • Validator used by $validatorName ( @Validator() annotation tagged)
  • $fields field to be verified, all fields of null validator
  • $userValidators A custom validator that is used at the same time and supports two formats.

Use example

All parameter verification

 use Swoft\Validator\Annotation\Mapping\Validator;

$data = [
    'email' => 'swoft@xx'
]

$result = validate($data, Validator::class); 

Specify field validation

 use Swoft\Validator\Annotation\Mapping\Validator;

$data = [
    'email' => 'swoft@xx'
]

$result = validate($data, Validator::class, ['email']); 

Use a custom validator at the same time

 use Swoft\Validator\Annotation\Mapping\Validator;

$data = [
    'start'  => 12,
    'end'    => 16,
];

$result = validate($data, Validator::class, [], ['testUserValidtor']); 

Use a custom validator and pass parameters

 use Swoft\Validator\Annotation\Mapping\Validator;

$data = [
    'start'  => 12,
    'end'    => 16,
    'params' => [1, 2]
];

$users = [
    'testUserValidtor' => [1, 2]
];

$result = validate($data, Validator::class, [], $users); 
/docs/2.x/en/validator/anywhere-validator.html
progress-bar