CodeIgniter Forums
Shield custom field - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Shield custom field (/showthread.php?tid=89020)



Shield custom field - elbambolo - 12-22-2023

Hello,
in codeigniter 4.
I'm using the codeigniter shield authorization and login system. I'm trying, following the official guide, to insert a custom user field. there is probably some step that wasn't clear to me or that I didn't perform correctly, because when I register a new user, the new custom field is not stored in the user table.

here my code. the custom field name are "cellulare"

I'm sure I'm missing a step, but I can't figure out which one. Thanks to everyone who will help me understand the problem.

migration:

PHP Code:
<?php

namespace App\Database\Migrations;

use 
CodeIgniter\Database\Forge;
use 
CodeIgniter\Database\Migration;

class 
AddMobileNumberToUsers extends Migration
{
    /**
    * @var string[]
    */
    private array $tables;

    public function __construct(?Forge $forge null)
    {
        parent::__construct($forge);

        /** @var \Config\Auth $authConfig */
        $authConfig  config('Auth');
        $this->tables $authConfig->tables;
    }

    public function up()
    {
        $fields = [
            'cellulare' => ['type' => 'VARCHAR''constraint' => '20''null' => true],
        ];
        $this->forge->addColumn($this->tables['users'], $fields);
    }

    public function down()
    {
        $fields = [
            'cellulare',
        ];
        $this->forge->dropColumn($this->tables['users'], $fields);
    }
}

Usermodel:

<?
php

declare(strict_types=1);

namespace 
App\Models;

use 
CodeIgniter\Shield\Models\UserModel as ShieldUserModel;

class 
UserModel extends ShieldUserModel
{

    protected function initialize(): void
    
{
        parent::initialize();

        $this->allowedFields = [
            ...$this->allowedFields,

            'cellulare',
        ];
    }
}

Validation:

<?
php

namespace Config;

use 
CodeIgniter\Config\BaseConfig;
use 
CodeIgniter\Validation\StrictRules\CreditCardRules;
use 
CodeIgniter\Validation\StrictRules\FileRules;
use 
CodeIgniter\Validation\StrictRules\FormatRules;
use 
CodeIgniter\Validation\StrictRules\Rules;

class 
Validation extends BaseConfig
{
    // --------------------------------------------------------------------
    // Setup
    // --------------------------------------------------------------------

    /**
    * Stores the classes that contain the
    * rules that are available.
    *
    * @var string[]
    */
    public array $ruleSets = [
        Rules::class,
        FormatRules::class,
        FileRules::class,
        CreditCardRules::class,
    ];

    /**
    * Specifies the views that are used to display the
    * errors.
    *
    * @var array<string, string>
    */
    public array $templates = [
        'list'  => 'CodeIgniter\Validation\Views\list',
        'single' => 'CodeIgniter\Validation\Views\single',
    ];

    // --------------------------------------------------------------------
    // Rules
    // --------------------------------------------------------------------

    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]',
            ],
        ],
        'cellulare' => [
            'label' => 'cellulare',
            'rules' => [
                'max_length[20]',
                'min_length[10]',
                'regex_match[/\A[0-9]+\z/]',
                'is_unique[users.cellulare]',
            ],
        ],
        '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]',
        ],
    ];




RE: Shield custom field - kenjis - 12-22-2023

This step?
https://github.com/codeigniter4/shield/blob/develop/docs/customization/user_provider.md#configuring-to-use-your-usermodel


RE: Shield custom field - elbambolo - 12-25-2023

(12-22-2023, 06:39 PM)kenjis Wrote: This step?
https://github.com/codeigniter4/shield/blob/develop/docs/customization/user_provider.md#configuring-to-use-your-usermodel
Yes, 
i followed this step.
i have named the field: 
cellulare
is the only field.
i have insert my usermodel.

it is wrong?


RE: Shield custom field - kenjis - 12-25-2023

My question was that did you do update the $userProvider in app/Config/Auth.php?
https://github.com/codeigniter4/shield/blob/develop/docs/customization/user_provider.md#configuring-to-use-your-usermodel

If you did it, I don't know what's wrong with you.


RE: Shield custom field - elbambolo - 12-27-2023

(12-25-2023, 05:03 PM)kenjis Wrote: My question was that did you do update the $userProvider in app/Config/Auth.php?
https://github.com/codeigniter4/shield/blob/develop/docs/customization/user_provider.md#configuring-to-use-your-usermodel

If you did it, I don't know what's wrong with you.
dho!
yes, but in the wrong way...

I followed the guide step by step.
I then called the Model, UserModel.php
when I came to edit the auth.php file I noticed that the directive was present:
public string $userProvider = UserModel::class;

I assumed that php spark not only created the UserMode.php file, but also put it in $UserProvider.

After reading your answer, I changed the variable to:

public string $userProvider = \App\Models\UserModel::class;

by entering the full path to the file created by php spark

now it works perfectly.
Thank you!


RE: Shield custom field - kenjis - 12-27-2023

Okay, good!

The official documentation has been updated:
https://shield.codeigniter.com/customization/user_provider/