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


Messages In This Thread
[SOLVED] Multi-Select or Arrayed Values Validation - by grimpirate - 04-19-2023, 10:00 AM



Theme © iAndrew 2016 - Forum software by © MyBB