Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter - Displaying individual error message for array fields

Have array of customers each with individual details. Here is a very SIMPLE example.

<input type="text" name="customer_names[]" />

In codeigniter, each customer_name is required
$this->form_validation->set_rules('customer_names[]','Customer Names','required');

If any of the customer names are blank, validation_errors(); shows one message for the entire array.

How can I obtain individual error messages for that customer?

NOTE: echo form_error('customer_names[0]'); is what I am trying to achieve where customer_name 0 was left blank.

like image 459
csi Avatar asked Nov 13 '22 13:11

csi


1 Answers

Looking at the Form Validation documentation, specifically the Using Arrays as Field Names section, I think you're going to need to explicitly name your inputs by including the index in the name to get the form_error() method to work as you desire.

So in order for form_error('customer_names[0]') to work, there will actually have to be an input with the name customer_names[0].

like image 119
birderic Avatar answered Jan 05 '23 00:01

birderic