跳至内容

自定义验证规则

注册

Shield 默认情况下具有以下注册规则

[
    'username' => [
        'label' => 'Auth.username',
        'rules' => [
            'required',
            'max_length[30]',
            'min_length[3]',
            'regex_match[/\A[a-zA-Z0-9\.]+\z/]',
            'is_unique[users.username]',
        ],
    ],
    'email' => [
        'label' => 'Auth.email',
        'rules' => [
            'required',
            'max_length[254]',
            'valid_email',
            'is_unique[auth_identities.secret]',
        ],
    ],
    'password' => [
        'label' => 'Auth.password',
        'rules' => [
                'required',
                'max_byte[72]',
                'strong_password[]',
            ],
        'errors' => [
            'max_byte' => 'Auth.errorPasswordTooLongBytes'
        ]
    ],
    'password_confirm' => [
        'label' => 'Auth.passwordConfirm',
        'rules' => 'required|matches[password]',
    ],
];

注意

如果你自定义表名,则上述规则中的表名(usersauth_identities)将自动更改。这些规则在 RegisterController::getValidationRules() 中实现。

如果你需要一组不同的注册规则,可以在 Validation 配置(app/Config/Validation.php)中指定它们,如下所示

//--------------------------------------------------------------------
// Rules For Registration
//--------------------------------------------------------------------
public $registration = [
    'username' => [
        'label' => 'Auth.username',
        'rules' => [
            'required',
            'max_length[30]',
            'min_length[3]',
            'regex_match[/\A[a-zA-Z0-9\.]+\z/]',
            'is_unique[users.username]',
        ],
    ],
    'email' => [
        'label' => 'Auth.email',
        'rules' => [
            'required',
            'max_length[254]',
            'valid_email',
            'is_unique[auth_identities.secret]',
        ],
    ],
    'password' => [
        'label' => 'Auth.password',
        'rules' => 'required|max_byte[72]|strong_password[]',
        'errors' => [
            'max_byte' => 'Auth.errorPasswordTooLongBytes'
        ]
    ],
    'password_confirm' => [
        'label' => 'Auth.passwordConfirm',
        'rules' => 'required|matches[password]',
    ],
];

注意

如果你自定义表名,请在规则中设置正确的表名。

登录

类似于注册部分中的验证规则流程,你可以将登录表单的规则添加到 app/Config/Validation.php 并更改规则。

//--------------------------------------------------------------------
// Rules For Login
//--------------------------------------------------------------------
public $login = [
    // 'username' => [
    //     'label' => 'Auth.username',
    //     'rules' => [
    //         'required',
    //         'max_length[30]',
    //         'min_length[3]',
    //         'regex_match[/\A[a-zA-Z0-9\.]+\z/]',
    //     ],
    // ],
    'email' => [
        'label' => 'Auth.email',
        'rules' => [
            'required',
            'max_length[254]',
            'valid_email'
        ],
    ],
    'password' => [
        'label' => 'Auth.password',
            'rules' => [
                'required',
                'max_byte[72]',
            ],
        'errors' => [
            'max_byte' => 'Auth.errorPasswordTooLongBytes',
        ]
    ],
];