CodeIgniter Forums
dynamic input field array validation. - 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: dynamic input field array validation. (/showthread.php?tid=65917)



dynamic input field array validation. - greenarrow - 08-09-2016

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.


RE: dynamic input field array validation. - cartalot - 08-09-2016

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; } 



RE: dynamic input field array validation. - greenarrow - 08-10-2016

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