5

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.

1

2 Answers 2

1

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].

Sign up to request clarification or add additional context in comments.

2 Comments

This worked fine for me, although I must explicitly name my fields with their respective keys in order to be useful. This answer here says that CodeIgniter 3 has a feature for this.
The answer of @machineaddict is just the same to this but I did not need to specify index in the rule name.
0

I was having the same problem with CodeIgniter 2.1.3. I have resolved it like this:

The input is:

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

The form validation is:

$this->form_validation->set_rules('customer_names[0]','Customer Names','required');
$this->form_validation->set_rules('customer_names[1]','Customer Names','required');
...

The errors are displayed like this:

echo form_error('customer_names[0]');
echo form_error('customer_names[1]');
...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.