CodeIgniter Forums
Calling setRules multiple times possible? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Calling setRules multiple times possible? (/showthread.php?tid=82962)



Calling setRules multiple times possible? - groovebird - 09-06-2022

Hi,

i want to add an array of additional validation rules to en existing array of rules.

$validation->setRules($firstArrayWithRules);
$validation->setRules($secondArraywithRules);

If i call this in the controller only the second array with rules will be executed and the first rules are ignored.

Is there something like addRules, where i can add additional rules to the rules set by setRules method?


RE: Calling setRules multiple times possible? - superior - 09-06-2022

Like this?
https://www.php.net/manual/en/function.array-merge.php


RE: Calling setRules multiple times possible? - groovebird - 09-06-2022

(09-06-2022, 06:15 AM)superior Wrote: Like this?
https://www.php.net/manual/en/function.array-merge.php

Yes, i did it this way but thougt there was an codeigniter native functionality for the validation rules. With a helper function in my controller i collect all rules and pass it to the setRules method. I think if i call the setRules method twice, the first rules are overwritten and set new by the second call


RE: Calling setRules multiple times possible? - superior - 09-07-2022

So why don't you build the array first before sending this to the setRules() method?


RE: Calling setRules multiple times possible? - ozornick - 09-08-2022

Logically, the "setX" action should overwrite the previous value. To add to lists it is possible to have "addX"
So create an array before validation
P.S. Not really an answer, but a hint