Introduction

The validator is a subclass of the rule manager and can be inherited directly if you only need to manage the rules.

class UserRulesManager extends RuleManager
{
    protected $rule = [
        'user'    => 'required|email',
        'pass'    => 'required|lengthBetween:6,16',
        'name'    => 'required|chs|lengthBetween:2,4',
        'remark'  => 'required|alpha_dash',
        'captcha' => 'required|length:4|checkCaptcha',
    ];

    protected $scene = [
        'login'   => ['user', 'pass'],
        'captcha' => ['captcha']
    ];

    protected $customAttributes = [
        'user'    => 'User Name',
        'pass'    => 'Password',
        'name'    => 'Nickname',
        'remark'  => 'Remarks',
        'captcha' => 'Captcha',
    ];

    protected $message = [
        'captcha.checkCaptcha' => 'Captcha error',
        'user.email'           => 'User name must be email',
        'pass.lengthBetween'   => 'Wrong password length'
    ];

    protected function sceneRegister(RuleManagerScene $scene)
    {
        return $scene->only(['user', 'pass', 'name', 'remark'])
            ->remove('remark', 'required|alpha_dash')
            ->append('remark', 'chs');
    }

    protected function sceneRegisterNeedCaptcha(RuleManagerScene $scene)
    {
        return $this->sceneRegister($scene)->appendCheckField('captcha');
    }

    public function ruleCheckCaptcha($att, $value): bool
    {
        return true;
    }
}

TIP

In the Rule Manager, rules and error messages can be extended in the way mentioned in the section on custom rules

Get rules

You can directly call the static method get of a class to get rules, error messages, and custom attributes

public static function get($fields = null, bool $initial = false): array
  • $fields Name of the field to get, or all rules if null
  • $initial Whether to get the original rule, the default is false, that is, the rule after parsing
UserRulesManager::get('user');

Note

If you want to apply the rules of the validation scene, please use the scene method after instantiating the class to specify the validation scene and then call the get method

(new UserRulesManager())->scene('register')->get('user');

Will return to

Array
(
    [0] => Array
        (
            [user] => Array
                (
                    [0] => required
                    [1] => email
                )

        )

    [1] => Array
        (
            [user.email] => User name must be email
        )

    [2] => Array
        (
            [user] => User Name
        )

)

To get the names of all rules, error messages, and custom attributes under a given validation scene, use the getBySceneName static method directly at

UserRulesManager::getBySceneName('register');

You can also call static methods directly

UserRulesManager::register();

Obtain separately

If you want to get the rules, error messages, and custom attributes individually, you can instantiate the rule manager and then use the following methods.

  • getRules Get validate rules
  • getCustomAttributes Get custom attributes
  • getMessages Get error message

Note

The getRules method is affected by the scene method, and the rules taken out may be different in different scenes.