CodeIgniter Forums
form validation in_list - 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: form validation in_list (/showthread.php?tid=71947)



form validation in_list - sh1ny - 10-16-2018

Hi,

Is there any way to pass an array into in_list[] form validation?

i.e. If I want to verify a value returned from a form exists in a DB column, and I get the column values as an array of values (no keys), is there a way to get in_list to read the array rather than hardcoding the values?

Thanks


RE: form validation in_list - jreklund - 10-16-2018

Yep, but you need to implode it first. So that your array turns into comma separation instead.

PHP Code:
$array = [=> 'red'=> 'green'=> 'blue'];
$array implode($array,',');
// Array now looks like:
$array 'red,green,blue';

// Use it like this
// I'm using array_keys() here because I want 0,1,2 instead of red,green,blue.
array(
    
'field' => 'menus_department[]',
    
'label' => lang('groups_type_department'),
    
'rules' => array(
        
'trim',
        
'required',
        
'in_list['.implode(array_keys($this->data['groups']['department']),',').']'
    
)
), 



RE: form validation in_list - sh1ny - 10-16-2018

(10-16-2018, 08:40 AM)jreklund Wrote: Yep, but you need to implode it first. So that your array turns into comma separation instead.

PHP Code:
$array = [=> 'red'=> 'green'=> 'blue'];
$array implode($array,',');
// Array now looks like:
$array 'red,green,blue';

// Use it like this
// I'm using array_keys() here because I want 0,1,2 instead of red,green,blue.
array(
    
'field' => 'menus_department[]',
    
'label' => lang('groups_type_department'),
    
'rules' => array(
        
'trim',
        
'required',
        
'in_list['.implode(array_keys($this->data['groups']['department']),',').']'
    
)
), 

Thanks for the answer, it's greatly appreciated.