CodeIgniter Forums
[SOLVED] Multi-Select or Arrayed Values Validation - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: [SOLVED] Multi-Select or Arrayed Values Validation (/showthread.php?tid=87434)



[SOLVED] Multi-Select or Arrayed Values Validation - grimpirate - 04-19-2023

I've tried the following two pieces of code (I've hardcoded the "passed" values into the run method for the sake of clarity):
PHP Code:
$validation = \Config\Services::validation();
$validation->setRule('groups''Groups', ['in_list[manager,personnel]']);
if(!
$validation->run(['groups' => ['manager''personnel']]))
        return redirect()->back()->withInput(); 

AND:
PHP Code:
$validation = \Config\Services::validation();
$validation->setRule('groups[]''Groups', ['in_list[manager,personnel]']);
if(!
$validation->run(['groups' => ['manager''personnel']]))
        return redirect()->back()->withInput(); 

The idea here is that a user is selecting multiple values in a drop down and submitting them via form. However, the return call is always being executed denoting an error. How do I create a rule that validates an array of values for one field?
The following works, but appears an inelegant solution:
PHP Code:
$validation = \Config\Services::validation();
$validation->setRule('groups[]''Groups', ['in_list[manager,personnel]']);
foreach([
'manager''personnel'] as $group)
        if(!$validation->run(['groups' => $group]))
                return redirect()->back()->withInput(); 



RE: Multi-Select or Arrayed Values Validation - kenjis - 04-19-2023

See https://codeigniter4.github.io/CodeIgniter4/libraries/validation.html#setting-rules-for-array-data


RE: Multi-Select or Arrayed Values Validation - grimpirate - 04-21-2023

Thank you kenjis, I completely overlooked this section.