Validation event class

The currently supported validation events are :

  • Pre-validation EventsbeforeValidate
  • Post-validation EventsafterValidate

Events are only supported to be defined for use in scene

Validator middleware requires inheritance W7\Validate\Support\Event\ValidateEventAbstract

The event class has three class properties:

  • $sceneName Current scene name
  • $data Value before validation or value after validation
  • $message Error message, if the event returns false, the validator will take this value as the error message
abstract class ValidateEventAbstract implements ValidateEventInterface
{
    public function beforeValidate(): bool;
    public function afterValidate(): bool;
}
1
2
3
4
5

The values passed can be obtained in the scene event handler class constructor.

If handler(CheckPermission::class,1,2,3) is used in the scenario, the constructor

public function __construct($a,$b,$c)
{
    
}
1
2
3
4

can get $a = 1 $b = 2 $c = 3 in that order.

Scene events

The event keyword can be used in the validation scenario, when the event is a scene event.

If you don't need to pass values to the scene event handler class:

protected $scene = [
    'add'    => ['id','user_id','role','event' => CheckPermission::class],
];
1
2
3

If you need to pass values to the scene event handler class:

protected $scene = [
    'add'    => ['id','user_id','role','event' => [
        CheckPermission::class=>[1,2,3,4]
    ]],
];
1
2
3
4
5

There is no limit to the number of values that can be passed, and they can be obtained in the constructor of the scene event handler class.

Assigning events to custom scenes

In custom validation scenarios, the event method can be used to define the scenario event handling class to be used.Unlimited number.

/*
* @method event(string $handler,...$params)
*/

$scene->event(CheckPermission::class)
1
2
3
4
5

If you want to pass values

$scene->event(CheckPermission::class,1,2,3)
1

Use multiple scene event handling classes

$scene->event(CheckPermission::class,1,2,3)->event(CheckName::class)
1

Global Events

If you need the validator to perform some actions before and after validation, you will need to use global events.

protected $event = [
    CheckPermission::class => ['owner']
];
1
2
3

Multiple global event handlers can be defined.

Scene event methods

If you need to perform some actions before or after the validation of the scenario, you can simply define after and before, in addition to using event to specify an event class. Instead of defining a class

In custom validation scenarios, you can use the after and before methods to define the event handling methods to be used.Unlimited number.

  • Before scene validation event before
  • After scene validation event after

Method accepts a validation data array $data parameter

The order of execution when used with the event class event is: beforeValidate->before->after->afterValidate

TIP

If you want to change the execution order, you can use the setEventPriority method in a custom validate scene

Methods are limited to methods of this class, and the naming convention for methods is after or before plus the method name. For example:

class LoginValidate extends \W7\Validate\Validate
{
    protected $rule = [
        'name' => 'required|chs',
        'user' => 'required|alpha_dash',
        'pass' => 'required|min:8',
    ];
    
    protected $scene = [
        'register' => ['name', 'user', 'pass', 'before' => 'checkRegisterStatus']
    ];
    
    public function beforeCheckRegisterStatus(array $data)
    {
        return true;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

The scene event method can also pass parameters for the method, the same method of passing parameters, except that the class name should be replaced by the method name, and the first parameter is the current validation value or the value after validation

protected $message = [
    'user.required' => 'user is required'
];

protected $scene = [
    'register' => ['name', 'user', 'pass', 'before' => 'setDefaultName','after'=> [
        'checkUserExist' => [1,2,3,4]
    ]]
];

public function afterCheckUserExist(array $data,$a,$b,$c)
{
    return true;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

Again, can get $a = 1 $b = 2 $c = 3 in that order. Closure and callable are also supported in custom validation scenarios

protected function sceneLogin(\W7\Validate\Support\ValidateScene $scene)
{
    $scene->only(['user', 'pass', 'captcha'])
        ->before(function ($data) {
            // TODO::Verify that the captcha is correct
        })
        ->after(function ($data) {
            // TODO::Verify that the account password is correct
        });
}
1
2
3
4
5
6
7
8
9
10

Return Value

If the returned string is a string, the W7\Validate\Exception\ValidateException exception is thrown, representing a failure, If the validation passes, return $next($data);

The event can return the key value of message directly, such as user.required, the validator will automatically look for the corresponding error message.

Last Updated: 10/13/2021, 2:39:38 PM