Welcome Guest, Not a member yet? Register   Sign In
Allowing parameters in custom validation rules function
#1

According to the documentation (https://codeigniter4.github.io/CodeIgnit...parameters) you can create a custom rule function that accepts parameters but no example of this rule being called is given, so it's unclear how it should be called.

For example, if I have the following function:

PHP Code:
public function foo(string $barstring $param, array $formDatastring $customError): bool 

what does the validate call need to look like? I don't see how to add parameters to foo in the call below.

PHP Code:
if(!$this->validate(['bar' => 'required|foo'])) 
Reply
#2

Perhaps a clearer question is this: https://codeigniter4.github.io/userguide...parameters

The example function they give in that link is called required_with. What would it look like to call required_with as a rule in the validation? They don't give this in the example, so it seems abstract to me.
Reply
#3

(This post was last modified: 12-02-2022, 10:36 PM by luckmoshy.)

do like this......... eg Tongue

Code:
public function validateUser(string $str, string $fields, array $data){
    $model = new UserModel();
    $user = $model->where('email', $data['email'])
                  ->first();

    if(!$user)
      return false;

    return password_verify($data['password'], $user['password']);
  }
then validation..
Code:
$rules = [
                'email' => 'required|min_length[6]|max_length[50]|valid_email',
                'password' => 'required|min_length[8]|max_length[255]|validateUser[email,password]',
            ];

            $errors = [
                'password' => [
                    'validateUser' => 'Email or Password don\'t match'
                ]
            ];

            if (! $this->validate($rules, $errors)) {
                $data['validation'] = $this->validator;
            }
Codeigniter First, Codeigniter Then You!!
yekrinaDigitals

Reply




Theme © iAndrew 2016 - Forum software by © MyBB