Welcome Guest, Not a member yet? Register   Sign In
Shield custom field
#1

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]',
        ],
    ];

Reply
#2

This step?
https://github.com/codeigniter4/shield/b...-usermodel
Reply
#3

(12-22-2023, 06:39 PM)kenjis Wrote: This step?
https://github.com/codeigniter4/shield/b...-usermodel
Yes, 
i followed this step.
i have named the field: 
cellulare
is the only field.
i have insert my usermodel.

it is wrong?
Reply
#4

My question was that did you do update the $userProvider in app/Config/Auth.php?
https://github.com/codeigniter4/shield/b...-usermodel

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

(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/b...-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!
Reply
#6

Okay, good!

The official documentation has been updated:
https://shield.codeigniter.com/customiza..._provider/
Reply




Theme © iAndrew 2016 - Forum software by © MyBB