October: OctoberCMS adding backend administrator fields with validation

Created on 19 May 2017  路  3Comments  路  Source: octobercms/october

I want to add few more fields in my backend administrator by creating my own plugin called as Users. Here is what I have done so far to create few new fields.

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

And now this is how my Administrator page looks like.

enter image description here

And if I click on "Team Recordfinder" I am able to see like below.

enter image description here

And if I select any of the team record from the list, its looks like below.

enter image description here

As you can see all working fine so far. But as soon as I fill full form, I am getting this SQL error saying column not found. Like below.

"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, [email protected], 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

enter image description here

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

Additionally, I want to make validations for this particular field hence I did below.

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

But validations are not working either. How can I validate this field if some one checked "Property Consultant" from User Role ?

Lot of questions but I need a best way to approach towards this issue. Can someone guide me to make this thing work ?

Thanks

Question

Most helpful comment

You will need to add team_id to the backend_users table for what you're currently trying to do to work. Make sure if you go that route that you remove that column in your down() method of your migration.

An alternative is using a manyToMany relationship instead between teams and backend users (and artificially limiting it to users having only one team) which wouldn't require any changes to the backend_users table.

All 3 comments

Github issues really isn't the place for questions like this. Please use one of the many other avenues available instead of submitting new issues for your questions. That being said, I'll take a quick look at this for you

Forum: http://octobercms.com/forum
StackOverflow: http://stackoverflow.com/questions/tagged/octobercms
IRC: #october on Freenode or http://octobercms.com/chat
Slack: https://octobercms-slack.herokuapp.com/

You will need to add team_id to the backend_users table for what you're currently trying to do to work. Make sure if you go that route that you remove that column in your down() method of your migration.

An alternative is using a manyToMany relationship instead between teams and backend users (and artificially limiting it to users having only one team) which wouldn't require any changes to the backend_users table.

Ok. Thanks for the instructions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gergo85 picture gergo85  路  3Comments

ChVuagniaux picture ChVuagniaux  路  3Comments

oppin picture oppin  路  3Comments

EbashuOnHolidays picture EbashuOnHolidays  路  3Comments

mittultechnobrave picture mittultechnobrave  路  3Comments