Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Am I doing this wrong, or is there a bug in the CodeIgniter Form Validation library when using arrays as field names?

Here's my controller code to configure the rules:

// Previous address(es)         
$this->form_validation->set_rules('prev_house_number[]',    'House Number',     'trim|alpha_numeric');
$this->form_validation->set_rules('prev_abode[]',           'Abode',            'trim');
$this->form_validation->set_rules('prev_house_name[]',      'House Name',       'trim');
$this->form_validation->set_rules('prev_address_line_1[]',  'Address Line 1',   'required|trim');
$this->form_validation->set_rules('prev_address_line_2[]',  'Address Line 2',   'trim');
$this->form_validation->set_rules('prev_city[]',            'Town/City',        'required|trim');
$this->form_validation->set_rules('prev_county[]',          'County',           'trim');
$this->form_validation->set_rules('prev_postcode[]',        'Postcode',         'required|max_length[9]|trim');
$this->form_validation->set_rules('prev_country[]',         'Country',          'trim');
$this->form_validation->set_rules('prev_months[]',          'Previous Months',  'trim|integer');
$this->form_validation->set_rules('prev_years[]',           'Previous Years',   'trim|integer');

The user can input up to 5 previous addresses, as so:

Form validation

The code behind the Address Line 1 field looks like this:

<div class="input w100 f-left c-none">
    <input type="text" class="address_line_1 postcode_prev_address_1" id="prev_address_line_1[]" name="prev_address_line_1[]" value="<?php echo set_value('prev_address_line_1[]');?>"/>
    <label for="prev_address_line_1[]">Address Line 1 <span class="required">*</span></label>
</div>

Here's the problem: Assuming I have 5 addresses entered, and validation fails on the form, all five input areas will be correctly repopulated, as expected. However, CodeIgniter's validation will not work on arrayed inputs.

Here's a direct screenshot after validation:

form validation fail

As you can see, although it repopulates the form with the correct values, it does not apply the validation rules against them and does not seem to recognise their existence from within the controller.

What can I do?

Thanks!

Jack

like image 692
Jack Avatar asked Nov 04 '22 14:11

Jack


1 Answers

It looks like this is the commit that Phil is talking about: https://github.com/EllisLab/CodeIgniter/commit/5c561805bd9ae6a4ad5d202277c34a879617b683#system/libraries/Form_validation.php

Try pulling the code from the develop branch for the form validation library and see if it takes care of the issue.

like image 123
Chris Schmitz Avatar answered Nov 09 '22 03:11

Chris Schmitz