Welcome Guest, Not a member yet? Register   Sign In
ArgumentCountError trying to use validation
#1

(This post was last modified: 02-02-2023, 02:35 AM by objecttothis.)

I'm getting an error (below) that doesn't quite make sense to me.
CI4.3.1
MariaDB 10.4.27
PHP7.4.33
Windows 11
XAMPP 3.3.0

Code:
ArgumentCountError

Too few arguments to function CodeIgniter\Validation\Validation::__construct(), 0 passed in C:\Users\objec\PhpstormProjects\opensourcepos\vendor\codeigniter4\framework\system\Validation\Validation.php on line 596 and exactly 2 expected

In my Controller (\app\Controllers\Login.php) I have:
PHP Code:
            if (strtolower($this->request->getMethod()) !== 'post')
            {
                return 
view('login'$data);
            }

            
$rules = [
                
'username' => 'required'
            
];

            if(!
$this->validate($rules)
            {
                return 
view('login'$data);
            } 

On first load of the view, I have no issues because validate is not being called. When I click submit on the form it errors and the stack trace points to

PHP Code:
if(!$this->validate($rules

\CodeIgniter\Validation shows me the function
PHP Code:
$rules = [];

    protected function 
validate($rules, array $messages = []): bool
    
{
        
$this->setValidator($rules$messages);

        return 
$this->validator->withRequest($this->request)->run();
    } 

and that does clearly show that it takes two arguments. At first I thought there was a problem with the documentation because the example, like my code calls validation with a single empty array for $rules, not two arguments, but I think this is shown this way in the example because the second parameter gets passed as null when no 2nd parameter is specified. Regardless, even when I pass a value in the array and pass a message array I still get this error.

What's puzzling to me is that it's saying that I've passed zero arguments.

I'm seeing now that it's complaining about the Validation constructor, not the validate() function. The thing is, that I don't think I'm doing anything different from the example code in the CI4 documentation, so I don't understand why it's not getting the expected $config and $view

PHP Code:
public function __construct($configRendererInterface $view
Reply
#2

Code:
Too few arguments to function CodeIgniter\Validation\Validation::__construct(), 0 passed in C:\Users\objec\PhpstormProjects\opensourcepos\vendor\codeigniter4\framework\system\Validation\Validation.php on line 596 and exactly 2 expected

The first thing you do is to see the source code.
Reply
#3

Turns out it was a custom ruleset from CI3 that was causing it to puke. Removing it as a Ruleset causes validation to work properly.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB