Converting CI3 Validation with multiple parameters - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: Converting CI3 Validation with multiple parameters (/showthread.php?tid=81620) |
Converting CI3 Validation with multiple parameters - objecttothis - 03-28-2022 I'm converting a CI3 application to CI4 and having a little difficulty with validation. I've been using https://codeigniter.com/user_guide/libraries/validation.html and https://codeigniter.com/user_guide/installation/upgrade_validations.html but my situation is still a little unclear to me. My controller has a rule and the validation check: PHP Code: $this->validator->setRule('username', 'lang:login_username', 'required|login_check'); I've created a custom validation class PHP Code: namespace app\Libraries; The problem is that formerly I had access to the request variables, because the validation function existed in the controller. I think that I need to pass Code: $this->request->getPost('password'); Code: public function login_check(string $username, string $fields, array $data, string &$error = null): bool https://codeigniter.com/user_guide/libraries/validation.html#validation-placeholders seems to offer validation placeholders, so I don't know if I can use that in my custom rule and whether I can pass multiple parameters. It also doesn't give an example of the function signature for a custom rule with validation placeholders, so not real clear to me. Two bonus questions are whether or not app\Libraries\ is the best practice for where to place the custom rules classes? and the second is whether it would be best to have a separate custom validation class for each MVC triplet, or if I should have a separate custom validation rule class for each one? I lean toward having one because there is overlap, but that class might get large since I potentially have many custom validations. RE: Converting CI3 Validation with multiple parameters - ignitedcms - 03-28-2022 As far as I'm aware you can add a new rule to the config>validation, for the POST request data I suppose you could just pass that in via a string variable which is shown in the example, but to be honest haven't tested it as such. |