Welcome Guest, Not a member yet? Register   Sign In
Form Validation and Array
#1

[eluser]cdonate[/eluser]
Hi, guys.

I have form inputs in a page and depending on the user that is logged, some of them are disabled on the user side using javascript.

This is the code:

Code:
<?php $name1 = array('name' => 'name[]','id' => 'inputName1','value' => set_value('name[0]'),'maxlength' => '150','size' => '35');?>
<?php $name2 = array('name' => 'name[]','id' => 'inputName2','value' => set_value('name[1]'),'maxlength' => '150','size' => '35');?>
<?php $name3 = array('name' => 'name[]','id' => 'inputName3','value' => set_value('name[2]'),'maxlength' => '150','size' => '35');?>

I have a few questions. First, the set_value('name[0,1,2]') will work? And second, if I send to the codeigniter controller the size that the post array is supposed to be, can I make the form validation work?

Since the form validation uses the name of the form input and, in this case, the names are all the same, 'name[]', is there a way that it will know if the first, or second or last position of the array is empty?

What I mean is, the user has six inputs to fill and he fills only the first three and the last two, leaving the fifth one empty, can the form validation check and send the error message for the empty form field?

Thanks for the help!

Cheers!
#2

[eluser]Dede[/eluser]
Try generating validation rules on the fly.
Example, for 6 fields (name="name[]")
Code:
for($i=0; $i<=5; $i++){
$this->validate =
array(
    array(
     'field' => 'name['.$i.']',
     'label' => 'Name No.'. $i,
     'rules' => 'trim|required|xss_clean|max_length[150]'
     )  
    );
}

$this->form_validation->set_rules($this->validate);

Guess, you can use the same loop for setting the inputs on validation error.
#3

[eluser]CroNiX[/eluser]
In answer to the question, yes, it will know which entry in the array failed validation (it keeps track of the keys) and output the appropriate error message for that individual form element that failed. There is no need to use the index in validation rules. Just use the name of the field, which is "name[]", just like the user guide for form validation mentions in the "Using Arrays as Field Names" section.




Theme © iAndrew 2016 - Forum software by © MyBB