Welcome Guest, Not a member yet? Register   Sign In
form validation in_list
#1

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
Reply
#2

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']),',').']'
    
)
), 
Reply
#3

(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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB