• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dynamic input fields validation in codeigniter

#1
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.
Reply

#2
Search for jQuery validate
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

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

#4
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.
Reply

#5
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]');
  }

Reply

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

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

#8
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); 
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.