Skip to content

Commit a0dffd1

Browse files
committed
docs: add "Adding Attributes to Users"
1 parent 7774592 commit a0dffd1

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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\Migration;
23+
24+
class AddMobileNumberToUsers extends Migration
25+
{
26+
public function up()
27+
{
28+
$fields = [
29+
'mobile_number' => ['type' => 'VARCHAR', 'constraint' => '20', 'null' => true],
30+
];
31+
$this->forge->addColumn('users', $fields);
32+
}
33+
34+
public function down()
35+
{
36+
$fields = [
37+
'mobile_number',
38+
];
39+
$this->forge->dropColumn('users', $fields);
40+
}
41+
}
42+
```
43+
44+
## Run Migrations
45+
46+
Run the migration file:
47+
48+
```console
49+
php spark migrate
50+
```
51+
52+
And check the `users` table:
53+
54+
```console
55+
php spark db:table users
56+
```
57+
58+
## Create UserModel
59+
60+
See [Customizing User Provider](./user_provider.md).
61+
62+
## Update Validation Rules
63+
64+
You need to update the [validation rules](./validation_rules.md) for registration.
65+
66+
If you do not add the validation rules for the new fields, the new field data will
67+
not be saved to the database.
68+
69+
Add the `$registration` property with the all validation rules for registration
70+
in **app/Config/Validation.php**:
71+
72+
```php
73+
<?php
74+
75+
namespace Config;
76+
77+
use CodeIgniter\Config\BaseConfig;
78+
use CodeIgniter\Validation\StrictRules\CreditCardRules;
79+
use CodeIgniter\Validation\StrictRules\FileRules;
80+
use CodeIgniter\Validation\StrictRules\FormatRules;
81+
use CodeIgniter\Validation\StrictRules\Rules;
82+
83+
class Validation extends BaseConfig
84+
{
85+
// ...
86+
87+
// --------------------------------------------------------------------
88+
// Rules
89+
// --------------------------------------------------------------------
90+
public $registration = [
91+
'username' => [
92+
'label' => 'Auth.username',
93+
'rules' => [
94+
'required',
95+
'max_length[30]',
96+
'min_length[3]',
97+
'regex_match[/\A[a-zA-Z0-9\.]+\z/]',
98+
'is_unique[users.username]',
99+
],
100+
],
101+
'mobile_number' => [
102+
'label' => 'Mobile Number',
103+
'rules' => [
104+
'max_length[20]',
105+
'min_length[10]',
106+
'regex_match[/\A[0-9]+\z/]',
107+
'is_unique[users.mobile_number]',
108+
],
109+
],
110+
'email' => [
111+
'label' => 'Auth.email',
112+
'rules' => [
113+
'required',
114+
'max_length[254]',
115+
'valid_email',
116+
'is_unique[auth_identities.secret]',
117+
],
118+
],
119+
'password' => [
120+
'label' => 'Auth.password',
121+
'rules' => [
122+
'required',
123+
'max_byte[72]',
124+
'strong_password[]',
125+
],
126+
'errors' => [
127+
'max_byte' => 'Auth.errorPasswordTooLongBytes'
128+
]
129+
],
130+
'password_confirm' => [
131+
'label' => 'Auth.passwordConfirm',
132+
'rules' => 'required|matches[password]',
133+
],
134+
];
135+
}
136+
```
137+
138+
## Customize Register View
139+
140+
1. Change the `register` view file in the **app/Config/Auth.php** file.
141+
142+
```php
143+
public array $views = [
144+
// ...
145+
'register' => '\App\Views\Shield\register',
146+
// ...
147+
];
148+
```
149+
150+
2. Copy file **vendor/codeigniter4/shield/src/Views/register.php** to **app/Views/Shield/register.php**.
151+
3. Customize the registration form to add the new fields.
152+
153+
```php
154+
<!-- Mobile Number -->
155+
<div class="form-floating mb-2">
156+
<input type="tel" class="form-control" id="floatingMobileNumberInput" name="mobile_number" autocomplete="tel" placeholder="Mobile Number (without hyphen)" value="<?= old('mobile_number') ?>">
157+
<label for="floatingMobileNumberInput">Mobile Number (without hyphen)</label>
158+
</div>
159+
```

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ nav:
111111
- customization/redirect_urls.md
112112
- customization/validation_rules.md
113113
- customization/user_provider.md
114+
- customization/adding_attributes_to_users.md
114115
- customization/extending_controllers.md
115116
- customization/integrating_custom_view_libs.md
116117
- customization/login_identifier.md

0 commit comments

Comments
 (0)