dynamic input fields validation in codeigniter |
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=""> form 3 Code: <input class="input_fn" type="text" name="ID3_first_name" id="ID3_first_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.
Search for jQuery validate
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
Thanks, but is there a way i can do this with ajax & codeigniter form rules?
You can still use the input posts during validation. For instance you can do something like:
PHP Code: // Expecting this one Then if they are posted they are validated. Best wishes, Paul.
If it's not predictable how many dynamically posted fields there will be, use this:
PHP Code: foreach ($this->input->post() as $key=>$value) {
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
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.
Wouter60's answer is the best solution offered so far. Another alternative would be to do something like this:
PHP Code: $x = 1; |
Welcome Guest, Not a member yet? Register Sign In |