Welcome Guest, Not a member yet? Register   Sign In
Problem with custom validation

Hello all. 

I'm new to CI4. I'm really excited about the new way of coding. I use CI3 for the last 6 years. 

I began to rewriite an application from CI3 to CII4 but I stack with a problem using custom validation.

I have added my custom validation file in App\Config\Validation.php

    public $ruleSets = [

This is the VisitorsAreaRules.php. The password in the db is not encrypted, is just varchar for now. That's why I don't use password_verify.

namespace App\Validation;

use App\Models\VisitorAreasModel;

class VisitorAreasRules {

  public function validateVisitor (string $str, string $fields, array $data): bool  {
    $visitorAreasModel = new VisitorAreasModel();
    $visitor = $visitorAreasModel->where('username', $data['username'])
                                 ->where('pass', $data['password'])
    if (!$visitor) {
      return false;
    return true;
  } // validateVisitor()
} // class

And this is my class
<?php namespace App\Controllers\World\Parents;

use App\Models\VisitorAreasModel;
use CodeIgniter\Controller;

class Login extends Controller
  public function index($alias = false)
    $data = [];

    $data['page_title'] = 'Parents Area';

    if ($this->request->getMethod() == 'post') {
      // Validation rules
      $rules = [
        'username'  => 'required',
        'password'  => 'required|validateVisitor(username, password)'

      $errors = [
        'password' => [
          'validateVisitor' => 'Invalid username or password'

      if (! $this->validate($rules, $errors)) {
        $data['validation'] = $this->validator;
      else {
        $visitorAreasModel = new VisitorAreasModel();

    return view('public/parents/login', $data);

  } // index()
} // class

It returns the bellow error:
validateVisitor(username, password) is not a valid rule.

Please, can anyone tell me what I do wrong?

Thank you in advance.

This is the error message that returns. I tried some changes but still I cannot find where is the error.

[Image: ci4-error.png]

It was typo error. The problem were in my controller file. I use ( ) instead of [ ] when I pass the fields in validateVisitor function.

Theme © iAndrew 2016 - Forum software by © MyBB