File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -81,3 +81,32 @@ class LoginController extends ShieldLogin
8181 }
8282}
8383```
84+
85+ ## Custom validation rules
86+
87+ ### Registration
88+
89+ Shield has the following rules for registration:
90+
91+ ``` php
92+ [
93+ 'username' => 'required|alpha_numeric_space|min_length[3]|is_unique[users.username]',
94+ 'email' => 'required|valid_email|is_unique[auth_identities.secret]',
95+ 'password' => 'required|strong_password',
96+ 'password_confirm' => 'required|matches[password]',
97+ ];
98+ ```
99+
100+ If you need a different set of rules for registration, you can specify them in your ` Validation ` configuration (** app\Config\Validation.php** ) like:
101+
102+ ``` php
103+ //--------------------------------------------------------------------
104+ // Rules
105+ //--------------------------------------------------------------------
106+ public $registration = [
107+ 'username' => 'required|alpha_numeric_space|min_length[3]|is_unique[users.username]',
108+ 'email' => 'required|valid_email|is_unique[auth_identities.secret]',
109+ 'password' => 'required|strong_password',
110+ 'password_confirm' => 'required|matches[password]',
111+ ];
112+ ```
Original file line number Diff line number Diff line change 88use CodeIgniter \Shield \Authentication \Authenticators \Session ;
99use CodeIgniter \Shield \Entities \User ;
1010use CodeIgniter \Shield \Models \UserModel ;
11- use CodeIgniter \Validation \Validation ;
1211
1312/**
1413 * Class RegisterController
@@ -53,11 +52,8 @@ public function registerAction(): RedirectResponse
5352 // like the password, can only be validated properly here.
5453 $ rules = $ this ->getValidationRules ();
5554
56- /** @var Validation $validation */
57- $ validation = service ('validation ' );
58-
5955 if (! $ this ->validate ($ rules )) {
60- return redirect ()->back ()->withInput ()->with ('errors ' , $ validation ->getErrors ());
56+ return redirect ()->back ()->withInput ()->with ('errors ' , $ this -> validator ->getErrors ());
6157 }
6258
6359 // Save the user
@@ -134,7 +130,7 @@ protected function getUserEntity(): User
134130 */
135131 protected function getValidationRules (): array
136132 {
137- return [
133+ return setting ( ' Validation.registration ' ) ?? [
138134 'username ' => 'required|alpha_numeric_space|min_length[3]|is_unique[users.username] ' ,
139135 'email ' => 'required|valid_email|is_unique[auth_identities.secret] ' ,
140136 'password ' => 'required|strong_password ' ,
You can’t perform that action at this time.
0 commit comments