CodeIgniter Forums

Full Version: dynamic input field array validation.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have  dynamic input field like this

PHP Code:
<input type="text" name="fault[0]" placeholder="" class="form-control name_list" /> 

this field can be increment like fault[1], fault[2], fault[3].......fault[10]  as many as the user willing to add the fields. how can i set validation rule for this?

code example please.
note the brackets after the field name value

PHP Code:
       $formrules = array(
           array(
               'field' => 'fault[]',
               'label' => 'the fault field',
               'rules' => 'trim|max_length[40]'
           ),
 
       // and whatever else you need 
 
       );
        $this->form_validation->set_rules($formrules);

       if ($this->form_validation->run() == true)
       {return true; }
       else
       
{ return false; } 
(08-09-2016, 06:41 PM)cartalot Wrote: [ -> ]note the brackets after the field name value

PHP Code:
       $formrules = array(
           array(
               'field' => 'fault[]',
               'label' => 'the fault field',
               'rules' => 'trim|max_length[40]'
           ),
 
       // and whatever else you need 
 
       );
        $this->form_validation->set_rules($formrules);

       if ($this->form_validation->run() == true)
       {return true; }
       else
       
{ return false; } 

Thanks mate this works.