Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Multi-Select or Arrayed Values Validation
#1

(This post was last modified: 04-21-2023, 08:05 AM by grimpirate.)

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(); 
Reply
#2

See https://codeigniter4.github.io/CodeIgnit...array-data
Reply
#3

Thank you kenjis, I completely overlooked this section.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB