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;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

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
1
  • $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');
1

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');
1

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
        )

)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

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');
1

You can also call static methods directly

UserRulesManager::register();
1

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.

Last Updated: 5/31/2021, 2:28:32 PM