codeigniter4 / CodeIgniter4

Open Source PHP Framework (originally from EllisLab)
https://codeigniter.com/
MIT License
5.4k stars 1.9k forks source link

Bug: Errors displayed in wrong order when using Validation:SetRule #7145

Closed Joubinho closed 1 year ago

Joubinho commented 1 year ago

PHP Version

7.4, 8.0, 8.1, 8.2

CodeIgniter4 Version

4.3.1

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

macOS, Windows

Which server did you use?

apache

Database

No response

What happened?

When using many SetRule from Validation service in order :

$validation->setRule('username', 'Username', 'required');

Then

$validation->setRule('password', 'Password', 'required');

Errors are shown in reverse order. ("Password error" then "Username error")

Steps to Reproduce

When using many SetRule from Validation service like that :

$validation = \Config\Services::validation();
$validation->setRule('username', 'Username', 'required');
$validation->setRule('password', 'Password', 'required');
if ($this->validation->withRequest($this->request)->run()) {...}

Submit your form and show errors in view with : <?= validation_list_errors() ?>

Expected Output

"Username error" then "Password error"

Anything else?

The problem seems to be in Validation/Validation.php line 446 When I replace :

$this->setRules($ruleSet + $this->getRules(), $this->customErrors);

By :

$this->setRules($this->getRules() + $ruleSet, $this->customErrors);

It's in the right order (but may cause other problem).

ddevsr commented 1 year ago

I send PR #7149