OctoberCMS用户插件如何拒绝保留名称

Mat*_*nis 1 octobercms

我正在使用用户插件.

这是我之前关于如何拒绝用户名更改的问题.

我有一个我不希望人们使用的保留名称列表(例如admin,anonymous,guest)我需要放入一个数组并在注册时拒绝.

我的自定义组件的Plugin.php

public function boot() {

    \RainLab\User\Models\User::extend(function($model) {

        $model->bindEvent('model.beforeSave', function() use ($model) {

            // Reserved Names List
            // Deny Registering if Name in List

        });

    });

}
Run Code Online (Sandbox Code Playgroud)

我如何使用Validator做到这一点?

Mit*_*ave 5

我们可以使用创建验证规则 Validator::extend():

Validator::extend('not_contains', function($attribute, $value, $parameters)
{
    // Banned words
    $words = array('a***', 'f***', 's***');
    foreach ($words as $word)
    {
        if (stripos($value, $word) !== false) return false;
    }
    return true;
});
Run Code Online (Sandbox Code Playgroud)

上面的代码定义了一个名为的验证规则not_contains- 它$words在fields字段中查找每个单词的存在,如果找到则返回false.否则返回true表示验证已通过.

然后我们可以正常使用我们的规则:

$rules = array(
    'nickname' => 'required|not_contains',
);

$messages = array(
    'not_contains' => 'The :attribute must not contain banned words',
);

$validator = Validator::make(Input::all(), $rules, $messages);

if ($validator->fails())
{
    return Redirect::to('register')->withErrors($validator);
}
Run Code Online (Sandbox Code Playgroud)

另请查看https://laravel.com/docs/5.4/validation#custom-validation-rules以了解如何在OctoberCMS中处理此问题.