CodeIgniter Forums
Using form validation with form field arrays?? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Using form validation with form field arrays?? (/showthread.php?tid=32060)



Using form validation with form field arrays?? - El Forum - 07-12-2010

[eluser]minerbog[/eluser]
Hi All, Didn't realise how many people use CI! How cool is that!!

Anyway's to my problem,

I am trying to validate a repopulate a multilined form using form field arrays. However I cannot get CI to do it for love nor money. Single form fields are a piece of cake!

Code:
function _form_validate
{
$this->form_validation->set_rules('description[]', 'Description', 'my_alpha_numeric');
$this->form_validation->set_rules('qty[]', 'Qty', 'my_alpha_numeric');
}

I'm using the above code to validate the fields and it seems to be working. But when I try
Code:
set_value('description[]');
as indicated by the user guide, it just returns a default value??

Please help guys.


Using form validation with form field arrays?? - El Forum - 07-12-2010

[eluser]minerbog[/eluser]
And just one more thing. When setting validation rules with form field arrays the same validation will not allow spaces on a form field thats an array but will on a form field that isn't an array??

Any ideas??


Using form validation with form field arrays?? - El Forum - 07-12-2010

[eluser]Luis Perez[/eluser]
I think that you need an array index to validate each form element.

Code:
<?php
    // count all array elements and loop for each one...
    for($i=0; $i<count($this->input->post('description')); $i++) ){
        // ... set their own rules
        $this->form_validation->set_rules('description['.$i.']', 'Description', 'my_alpha_numeric');
    }
?&gt;

I guess that if there's no index, CI validates only one element.


Using form validation with form field arrays?? - El Forum - 07-12-2010

[eluser]minerbog[/eluser]
Thanks Luis, I'll give it a try