Welcome Guest, Not a member yet? Register   Sign In
Validation with array field names ?
#1

[eluser]Juan Velandia[/eluser]
If I have this
Code:
<?php echo form_open('form'); ?>
<input type="text" name="number[]" value="" size="50" />
<input type="text" name="number[]" value="" size="50" />
<input type="text" name="number[]" value="" size="50" />
<input type="text" name="number[]" value="" size="50" />

</form>
How can I validate fields having in mind that all fields have the same name, and this rule doesnt apply because the user could fill 1, 2, 3 or all the 4 field.

Code:
$this->form_validation->set_rules('number', 'Number', 'required');

Any ideas? Thanks in advance!
#2

[eluser]Aken[/eluser]
Use a callback or extend the form validation library with your own rule(s). The default rules are not set up to handle arrays.
#3

[eluser]Juan Velandia[/eluser]
Plop, I think I will go with JS and getElementById, thanks
#4

[eluser]Aken[/eluser]
You shouldn't rely on JS by itself to validate data, since anyone can circumvent it by simply turning JS off.
#5

[eluser]LuckyFella73[/eluser]
I never tried but according to the user guide you can set
rules for arrays:


Code:
// example from User guide:
$this->form_validation->set_rules('options[]', 'Options', 'required');

http://ellislab.com/codeigniter/user-gui...ationrules

Section "Using Arrays as Field Names"


EDIT:
In your case that should work:
Code:
$this->form_validation->set_rules('number[]', 'Number', 'required');




Theme © iAndrew 2016 - Forum software by © MyBB