Validation event class

The currently supported validation events are :

  • Pre-validation EventsbeforeValidate
  • Post-validation EventsafterValidate

Validator middleware requires inheritance Itwmw\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;
}

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)
{
    
}

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],
];

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]
    ]],
];

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)

If you want to pass values

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

Use multiple scene event handling classes

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

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']
];

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 \Itwmw\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;
    }
}

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;
}

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(\Itwmw\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
        });
}

Return Value

If the returned string is a string, the Itwmw\Validate\Exception\ValidateException exception is thrown, representing a failure, If the validation passes, return True

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