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

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

Code:
    public $ruleSets = [
        \CodeIgniter\Validation\Rules::class,
        \CodeIgniter\Validation\FormatRules::class,
        \CodeIgniter\Validation\FileRules::class,
        \CodeIgniter\Validation\CreditCardRules::class,
        \App\Validation\VisitorAreasRules::class,
    ];

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.
Code:
<?php

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'])
                                 ->first();
    if (!$visitor) {
      return false;
    }
    return true;
  } // validateVisitor()
} // class


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

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

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

    $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.
Reply
#2

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

[Image: ci4-error.png]
Reply
#3

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




Theme © iAndrew 2016 - Forum software by © MyBB