OctoberCMS 添加带有验证的后端管理员字段

OctoberCMS adding backend administrator fields with validation

我想通过创建我自己的名为 Users 的插件在我的 后端管理员 中添加更多字段。这是我到目前为止为创建几个新字段所做的工作。

plugins\technobrave\users\Plugin.php

<?php namespace Technobrave\Users;

use System\Classes\PluginBase;
use Backend\Models\User as BackendUserModel;
use Backend\Controllers\Users as BackendUsersController;
class Plugin extends PluginBase
{
    public function registerComponents()
    {
    }

    public function registerSettings()
    {
    }

    public function boot()
    {
          // Add college and teacher field to Users table
        BackendUserModel::extend(function($model){
            $model->belongsTo['team'] = ['technobrave\team\Models\Team'];            

        });




         // Add college and teacher field to Users form
         BackendUsersController::extendFormFields(function($form, $model, $context){

            if (!$model instanceof BackendUserModel)
                return;

            $form->addTabFields([
                'team' => [
                    'label'   => 'Team',
                    'comment' => 'Associate this user with a team.',
                    'type' => 'recordfinder',
                    'list' => '$/technobrave/team/models/team/columns.yaml',
                    'prompt' => 'Click the %s to find a team',
                    'select' => 'id',
                    'nameFrom'=> 'name',
                    'tab' => 'Account'
                ]
            ]);
        });
    }
}

plugins\technobrave\users\models\User.php

<?php namespace Technobrave\Users\Models;

use Model;


/**
 * Model
 */
class User extends Model
{



    use \October\Rain\Database\Traits\Validation;

    /*
     * Validation
     */
    public $rules = [
        'team_id' => 'required',

    ];

    public $customMessages = [
                'team_id.required' => 'Please select Team',




    ];

    /**
     * @var string The database table used by the model.
     */
    public $table = 'backend_users';
}

这就是我的 管理员 页面的样子。

如果我点击 "Team Recordfinder",我会看到如下所示。

如果我 select 列表中的任何团队记录,其如下所示。

如您所见,到目前为止一切正常。但是,一旦我填写完整表格,我就会收到此 SQL 错误,提示未找到列。如下图。

"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'team_id' in 'field list' (SQL: insert into backend_users (is_superuser, login, email, first_name, last_name, password, persist_code, permissions, team_id, updated_at, created_at) values (0, Johny, johny@mailinator.com, Johny, Cook, y$bbr00J5O2dE1WDHHWZYHIeMduXI82HkDnE8IYBcAet4ie0nfpgpwq, , , 9, 2017-05-19 06:23:49, 2017-05-19 06:23:49))" on line 666 of C:\xampp\htdocs\slp_website_cms\vendor\laravel\framework\src\Illuminate\Database\Connection.php

现在,我的问题是,我需要在 backend_users table 中手动添加 team_id 字段吗?这是有效的方法吗?我不确定。

此外,我想对这个特定字段进行验证,因此我在下面做了。

plugins\technobrave\users\models\User.php

<?php namespace Technobrave\Users\Models;

use Model;


/**
 * Model
 */
class User extends Model
{



    use \October\Rain\Database\Traits\Validation;

    /*
     * Validation
     */
    public $rules = [
        'team_id' => 'required',

    ];

    public $customMessages = [
                'team_id.required' => 'Please select Team',




    ];

    /**
     * @var string The database table used by the model.
     */
    public $table = 'backend_users';
}

但是验证也不起作用。如果有人从 User Role 检查了 "Property Consultant",我该如何验证这个字段?

很多问题,但我需要一个最好的方法来解决这个问题。有人可以指导我使这件事起作用吗?

谢谢

do I need to add team_id field manually in backend_users table ? Is it valid way ? I am not sure.

是的,您需要创建迁移以将此字段添加到 table。

要添加验证规则,您还可以扩展模型:

public function boot() { 
    BackendUserModel::extend(function($model){
         $myrules = $model->rules;
         $myrules['team_id'] = 'required';
         $model->rules = $myrules;
    });
}

要获得登录用户,您可以使用 App::before

public function boot() { 
    \App::before(function() { 
        // Here BackendAuth::getUser() is already initialized 
    )};
}