CodeIgniter Forums
dynamic input fields validation in codeigniter - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: dynamic input fields validation in codeigniter (/thread-65722.html)



dynamic input fields validation in codeigniter - greenarrow - 07-16-2016

I'm working on a web application that includes a feature to add information of vehicles of users, if a user have more than one vehicle he/she can add those details by "add another" like option. i have main form which has 8 fields.


to duplicate the main form i am using using http://tristandenyer.com/demos/dynamic-form.html script. this script does what i want but the thing is i have no idea how to validate the from fields generated by this script.

by clicking "add section" option the script generates a copy of original form, the field names of form goes as "ID2_first_name" (ID2 mean second copy of original form if the form it goes like ID3_first_name)

ex:
form 2
Code:
<input class="input_fn" type="text" name="ID2_first_name" id="ID2_first_name" value="">

<input class="input_ln" type="text" name="ID2_last_name" id="ID2_last_name" value="">

form 3
Code:
<input class="input_fn" type="text" name="ID3_first_name" id="ID3_first_name" value="">

<input class="input_ln" type="text" name="ID3_last_name" id="ID3_last_name" value="">

....
if a user have 5 vehicles the field of the form named like ID2_first_name.....ID5_first_name.
how can i apply validation rules to these dynamic fields. i am using codeigniter mainly for this.


RE: dynamic input fields validation in codeigniter - InsiteFX - 07-16-2016

Search for jQuery validate


RE: dynamic input fields validation in codeigniter - greenarrow - 07-16-2016

Thanks, but is there a way i can do this with ajax & codeigniter form rules?


RE: dynamic input fields validation in codeigniter - PaulD - 07-16-2016

You can still use the input posts during validation. For instance you can do something like:

PHP Code:
// Expecting this one
$this->form_validation->set_rules('ID1_first_name''First Name''trim|required|max_length[50]');

// Optionally might be submitted ones
if($this->input->post(ID2_first_name$this->form_validation->set_rules('ID2_first_name''First Name''trim|required|max_length[50]');
if(
$this->input->post(ID3_first_name$this->form_validation->set_rules('ID3_first_name''First Name''trim|required|max_length[50]');
if(
$this->input->post(ID4_first_name$this->form_validation->set_rules('ID4_first_name''First Name''trim|required|max_length[50]');
if(
$this->input->post(ID5_first_name$this->form_validation->set_rules('ID5_first_name''First Name''trim|required|max_length[50]'); 

Then if they are posted they are validated.

Best wishes,

Paul.


RE: dynamic input fields validation in codeigniter - Wouter60 - 07-16-2016

If it's not predictable how many dynamically posted fields there will be, use this:
PHP Code:
foreach ($this->input->post() as $key=>$value) {
  if (! 
strpos('_first_name',$key) === FALSE ) {
    
$this->form_validation->set_rules($key'First Name''trim|required|max_length[50]');
  }




RE: dynamic input fields validation in codeigniter - greenarrow - 07-16-2016

The thing is name like ID2_first_name generate dynamically. have a look at this demo please http://tristandenyer.com/demos/dynamic-form.html

as you can see here this script duplicates the main form, so by using "Add section" option you will get a copy of original form. the name & id values will include the prefix ID2.....IDn

please check the demo using firebug


RE: dynamic input fields validation in codeigniter - PaulD - 07-17-2016

It makes no difference how many additional fields are added, you just need to check if they are being posted and if so validate them.


RE: dynamic input fields validation in codeigniter - mwhitney - 07-18-2016

Wouter60's answer is the best solution offered so far. Another alternative would be to do something like this:

PHP Code:
$x 1;
do {
    if (
$this->input->post("ID{$x}_first_name") || $this->input->post("ID{$x}_last_name"/* check all of your fields */) {
        
$this->form_validation->set_rules("ID{$x}_first_name"'First Name''trim|required|max_length[50]');
        
$this->form_validation->set_rules("ID{$x}_last_name"'Last Name''trim|required|max_length[50]');
        
// etc.
    
} else {
        break; 
// you could set $x = 0; instead, but by using break, $x is set to 1 more than the number on the last set of fields you validated.
    
}
} while (++
$x 0);