Welcome Guest, Not a member yet? Register   Sign In
Converting CI3 Validation with multiple parameters
#1

I'm converting a CI3 application to CI4 and having a little difficulty with validation.

I've been using https://codeigniter.com/user_guide/libra...ation.html and https://codeigniter.com/user_guide/insta...tions.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');
...
if (!
$this->validate([]))
{
 echo 
view('login', ['validation' => $this->validator]);


I've created a custom validation class
PHP Code:
namespace app\Libraries;

...
use 
app\Models\Employee;

/**
 ...
 * @property employee employee
 */
class MY_Validation
{
 
/**
 * Checks to make sure that the user is logged in or not.  Called as a validator rule.
 *
 * @param string $username
 * @param string|null $error
 * @return bool
 */
 
public function login_check(string $usernamestring &$error null): bool
 
{
 ...
 
$this->employee model('Employee');

>>>> 
$password $this->request->getPost('password');

 if(!
$this->_installation_check())
 {
 
$error lang('Login.invalid_installation');

 return 
FALSE;
 }
 ...
 if(!
$this->employee->login($username$password))
 {
 
$error lang('Login.invalid_username_and_password');

 return 
FALSE;
 }

 return 
TRUE;
 } 

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');
to the validator in the Controller, but it is unclear to me how this should be done.  https://codeigniter.com/user_guide/libra...parameters seems to indicate that my custom rule function signature should be
Code:
public function login_check(string $username, string $fields, array $data, string &$error = null): bool
but then doesn't show how the rule is called (passing said parameters) in the Controller.

https://codeigniter.com/user_guide/libra...aceholders 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.
Reply
#2

(This post was last modified: 03-28-2022, 09:35 AM by ignitedcms.)

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.
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply




Theme © iAndrew 2016 - Forum software by © MyBB