I need to create a form with the same group of fields (data is from database)
For example:
Group 1: Description field, Amount field, Others fields
Group 2: Description field, Amount field, Others fields
Group 3: Description field, Amount field, Others fields
So in my view I loop the database values:
$i = 0;
foreach ($data_from_db as $data) {
$description = array(
'name' => 'description[]',
'id' => 'description_field['.$i.']',
'value' => set_value('description[]', $data->description)
);
echo form_label(lang('reward_description'), 'description_field['.$i.']');
echo form_textarea($description);
echo form_error('description[]');
// more fields generated here in similar ways
$i++
}
In my controller I have:
$this->form_validation->set_rules('description[]', 'lang:project_edit_description', 'required');
// more similar rules here
According to the Codeigniter documentation this is the correct way to do it, but its no working correctly.
Codeigniter is getting confused with the validation messages.
For example, if I leave intentionally one of the descriptions fields empty and submit the form, I get the validation error message in all the groups (I get “the Description field is required” also for group 1, 2 and 3)
Just set rules for each indexed field. Following snippets show a working example (Improve it as you require):
Test controller:
Test view
views/test.php:This code will show
The description x field is required.only in the empty fields.