【问题标题】:OctoberCMS adding backend administrator fields with validationOctoberCMS 添加带有验证的后端管理员字段
【发布时间】:2017-10-19 03:58:39
【问题描述】:

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

插件\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”,我可以看到如下所示。

如果我从列表中选择任何团队记录,其如下所示。

如您所见,到目前为止一切正常。但是,一旦我填写完整的表格,我就会收到这个 SQL 错误,说找不到列。如下所示。

“SQLSTATE[42S22]: 未找到列: 1054 '字段列表'中的未知列 'team_id' (SQL: 插入到backend_users (is_superuser, login, email, first_name, last_namepasswordpersist_codepermissionsteam_idupdated_atcreated_at)的值(0,约翰尼拉,johny@mailinator.com,约翰尼拉,厨师,Y $ bbr00J5O2dE1WDHHWZYHIeMduXI82HkDnE8IYBcAet4ie0nfpgpwq,,,9 , 2017-05-19 06:23:49, 2017-05-19 06:23:49))" 在 C:\xampp\htdocs\slp_website_cms\vendor\laravel\framework\src\Illuminate\Database\ 的第 666 行连接.php

现在,我的问题是,我需要在 backend_users 表中手动添加 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';
}

但验证也不起作用。如果有人从用户角色中检查了“物业顾问”,我该如何验证此字段?

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

谢谢

【问题讨论】:

    标签: php octobercms octobercms-backend octobercms-plugins october-form-controller


    【解决方案1】:

    我需要在 backend_users 表中手动添加 team_id 字段吗?这是有效的方式吗?我不确定。

    是的,您需要创建迁移才能将此字段添加到表中。

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

    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 
        )};
    }
    

    【讨论】:

    • 也许添加规则“re​​quired_if:field,value,...”?
    • 由于字段“user_group_id”在另一个表(backend_userS_groups)中,您很可能需要为此octobercms.com/docs/services/validation#custom-validation-rules编写自定义验证规则
    • 对于登录用户,您可以使用 App::before public function boot() { \App::before(function() { // Here BackendAuth::getUser() is already initialized } }
    猜你喜欢
    • 1970-01-01
    • 2018-11-15
    • 1970-01-01
    • 1970-01-01
    • 2023-02-19
    • 2015-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多