I am sending dynamically created form fields to CodeIgniter via ajax.
<div class="form-element">
<input type="radio" id="delete-radio[1]" name="delete-radio[1]" value="1">
</div>
<div class="form-element">
<label for="member_id[1]">Membership ID</label>
<input type="text" id="member_id[1]" name="member_id[]" autocomplete="off" value="<?php echo set_value('member_id[1]'); ?>"/>
<?php echo form_error('member_id[1]', '<div class="error">', '</div>'); ?>
</div>
<div class="form-element">
<label for="member_name[1]">Name</label>
<input type="text" id="member_name[1]" name="member_name[]" autocomplete="off" value="<?php echo set_value('member_name[1]'); ?>"/>
<?php echo form_error('member_name[1]', '<div class="error">', '</div>'); ?>
</div>
<div class="form-element">
<label for="member_address[1]">Address</label>
<input type="text" id="member_address[1]" name="member_address[]" autocomplete="off" value="<?php echo set_value('member_address[1]'); ?>"/>
<?php echo form_error('member_address[1]', '<div class="error">', '</div>'); ?>
</div>
I use the following to send them to their controller:
$('#submit').click(function() {
var form_data = $('#ajax-form').serialize();
alert(form_data);
$.ajax({
url: "<?php echo site_url('ajax_ci/ajax_check'); ?>",
type: 'POST',
async : false,
data: form_data,
success: function(msg) {
$('#form-name').append(msg);
}
});
return false;
});
Here is the controller:
if ($this->input->is_ajax_request()) {
$this->form_validation->set_rules('member_id[]', 'member ID', 'trim|required|xss_clean');
$this->form_validation->set_rules('member_name[]', 'member name', 'trim|required|xss_clean');
$this->form_validation->set_rules('member_address[]', 'member password', 'trim|required|xss_clean');
if($this->form_validation->run() == FALSE) {
echo validation_errors();
} else {
// connect to database
}
} else {
echo "Work on it! Don't give up!";
}
The screenshot below is currently what happens.

But I want these errors to appear in their respective individual form_errors

Is this possible in my current setup?
In the validation errors that you return from the server, you will need to include information about which field failed. Probably an array of (fieldname=>errormsg). Your ajax response handler can then find the correct field and append the message to the DOM accordingly.