|
| 1 | +# Adding Attributes to Users |
| 2 | + |
| 3 | +If you need to add new attributes like phone numbers, employee or school IDs, etc. |
| 4 | +to users, one way is to add columns to `users` table. |
| 5 | + |
| 6 | +## Create Migration File |
| 7 | + |
| 8 | +Create a migration file to add new columns. |
| 9 | + |
| 10 | +You can easily create a file for it with the `spark` command: |
| 11 | +```console |
| 12 | +php spark make:migration AddMobileNumberToUsers |
| 13 | +``` |
| 14 | + |
| 15 | +And write code to add/drop columns. |
| 16 | + |
| 17 | +```php |
| 18 | +<?php |
| 19 | + |
| 20 | +namespace App\Database\Migrations; |
| 21 | + |
| 22 | +use CodeIgniter\Database\Forge; |
| 23 | +use CodeIgniter\Database\Migration; |
| 24 | + |
| 25 | +class AddMobileNumberToUsers extends Migration |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @var string[] |
| 29 | + */ |
| 30 | + private array $tables; |
| 31 | + |
| 32 | + public function __construct(?Forge $forge = null) |
| 33 | + { |
| 34 | + parent::__construct($forge); |
| 35 | + |
| 36 | + /** @var \Config\Auth $authConfig */ |
| 37 | + $authConfig = config('Auth'); |
| 38 | + $this->tables = $authConfig->tables; |
| 39 | + } |
| 40 | + |
| 41 | + public function up() |
| 42 | + { |
| 43 | + $fields = [ |
| 44 | + 'mobile_number' => ['type' => 'VARCHAR', 'constraint' => '20', 'null' => true], |
| 45 | + ]; |
| 46 | + $this->forge->addColumn($this->tables['users'], $fields); |
| 47 | + } |
| 48 | + |
| 49 | + public function down() |
| 50 | + { |
| 51 | + $fields = [ |
| 52 | + 'mobile_number', |
| 53 | + ]; |
| 54 | + $this->forge->dropColumn($this->tables['users'], $fields); |
| 55 | + } |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +## Run Migrations |
| 60 | + |
| 61 | +Run the migration file: |
| 62 | + |
| 63 | +```console |
| 64 | +php spark migrate |
| 65 | +``` |
| 66 | + |
| 67 | +And check the `users` table: |
| 68 | + |
| 69 | +```console |
| 70 | +php spark db:table users |
| 71 | +``` |
| 72 | + |
| 73 | +## Create UserModel |
| 74 | + |
| 75 | +See [Customizing User Provider](./user_provider.md). |
| 76 | + |
| 77 | +## Update Validation Rules |
| 78 | + |
| 79 | +You need to update the [validation rules](./validation_rules.md) for registration. |
| 80 | + |
| 81 | +If you do not add the validation rules for the new fields, the new field data will |
| 82 | +not be saved to the database. |
| 83 | + |
| 84 | +Add the `$registration` property with the all validation rules for registration |
| 85 | +in **app/Config/Validation.php**: |
| 86 | + |
| 87 | +```php |
| 88 | +<?php |
| 89 | + |
| 90 | +namespace Config; |
| 91 | + |
| 92 | +use CodeIgniter\Config\BaseConfig; |
| 93 | +use CodeIgniter\Validation\StrictRules\CreditCardRules; |
| 94 | +use CodeIgniter\Validation\StrictRules\FileRules; |
| 95 | +use CodeIgniter\Validation\StrictRules\FormatRules; |
| 96 | +use CodeIgniter\Validation\StrictRules\Rules; |
| 97 | + |
| 98 | +class Validation extends BaseConfig |
| 99 | +{ |
| 100 | + // ... |
| 101 | + |
| 102 | + // -------------------------------------------------------------------- |
| 103 | + // Rules |
| 104 | + // -------------------------------------------------------------------- |
| 105 | + public $registration = [ |
| 106 | + 'username' => [ |
| 107 | + 'label' => 'Auth.username', |
| 108 | + 'rules' => [ |
| 109 | + 'required', |
| 110 | + 'max_length[30]', |
| 111 | + 'min_length[3]', |
| 112 | + 'regex_match[/\A[a-zA-Z0-9\.]+\z/]', |
| 113 | + 'is_unique[users.username]', |
| 114 | + ], |
| 115 | + ], |
| 116 | + 'mobile_number' => [ |
| 117 | + 'label' => 'Mobile Number', |
| 118 | + 'rules' => [ |
| 119 | + 'max_length[20]', |
| 120 | + 'min_length[10]', |
| 121 | + 'regex_match[/\A[0-9]+\z/]', |
| 122 | + 'is_unique[users.mobile_number]', |
| 123 | + ], |
| 124 | + ], |
| 125 | + 'email' => [ |
| 126 | + 'label' => 'Auth.email', |
| 127 | + 'rules' => [ |
| 128 | + 'required', |
| 129 | + 'max_length[254]', |
| 130 | + 'valid_email', |
| 131 | + 'is_unique[auth_identities.secret]', |
| 132 | + ], |
| 133 | + ], |
| 134 | + 'password' => [ |
| 135 | + 'label' => 'Auth.password', |
| 136 | + 'rules' => [ |
| 137 | + 'required', |
| 138 | + 'max_byte[72]', |
| 139 | + 'strong_password[]', |
| 140 | + ], |
| 141 | + 'errors' => [ |
| 142 | + 'max_byte' => 'Auth.errorPasswordTooLongBytes', |
| 143 | + ] |
| 144 | + ], |
| 145 | + 'password_confirm' => [ |
| 146 | + 'label' => 'Auth.passwordConfirm', |
| 147 | + 'rules' => 'required|matches[password]', |
| 148 | + ], |
| 149 | + ]; |
| 150 | +} |
| 151 | +``` |
| 152 | + |
| 153 | +## Customize Register View |
| 154 | + |
| 155 | +1. Change the `register` view file in the **app/Config/Auth.php** file. |
| 156 | + |
| 157 | + ```php |
| 158 | + public array $views = [ |
| 159 | + // ... |
| 160 | + 'register' => '\App\Views\Shield\register', |
| 161 | + // ... |
| 162 | + ]; |
| 163 | + ``` |
| 164 | + |
| 165 | +2. Copy file **vendor/codeigniter4/shield/src/Views/register.php** to **app/Views/Shield/register.php**. |
| 166 | +3. Customize the registration form to add the new fields. |
| 167 | + |
| 168 | + ```php |
| 169 | + <!-- Mobile Number --> |
| 170 | + <div class="form-floating mb-2"> |
| 171 | + <input type="tel" class="form-control" id="floatingMobileNumberInput" name="mobile_number" autocomplete="tel" placeholder="Mobile Number (without hyphen)" value="<?= old('mobile_number') ?>"> |
| 172 | + <label for="floatingMobileNumberInput">Mobile Number (without hyphen)</label> |
| 173 | + </div> |
| 174 | + ``` |
0 commit comments