I have a form that has its fields dynamically added as such:
function addFieldRow() {
$('.form-container').append('
<div><input type="text" name="name[1]"></div>
<div>
<select name="choice[1]">
<option value="red">red</option>
// . . .
</select>
</div>
// . . .
');
}
I then pass these to its respective CodeIgniter controller with the declared array form_validation rules:
$this->form_validation->set_rules('name[]', 'text field', 'required');
// . . . other fields' rules
The problem is that if any of the dynamically added forms have an error, these are gone during the view loading. I want to know if how can I preserve the dynamically added form fields after the validation run with errors:
if ($this->form_validation->run() == FALSE) {
// do stuff to preserve the dynamically created fields to show their errors
$this->load->view('user_addresses_view');
}
I have found the solution to preserve these dynamically added elements by using ajax
to connect to the form's respective controller in the CodeIgniter framework. This method can:
Solution by james246
you can't do it because page is actually refreshing and row is added client side so when validation done then you will redirected to same page with validation errors you will not see dynamically added dom because as i said it was happend client side not in your code...so all you can do is use session
and then at load time manually validate and add them to page.
Using SESSION
might not be good idea because for that you will have to code more complex helpers and validation methods and etc...
Other possible solution
use jQuery.validate
plugin that allow us to validate dynamically added rows ( fields , dom ).
take benefit of mysql and add those field into database with possible error stored in db and send it to view file.