Welcome Guest, Not a member yet? Register   Sign In
loop over form elements?
#1

[eluser]jorre[/eluser]
Hi again.
Here's another beginner question from my side.

I'm using the code below to validate some 10 input fields and for refilling input fields with their respective values when an error occurs somewhere on the form.

As you see this code it already quite long and repetitive. I want to add input fields when a user clicks on a link, and these (not yet) added fields should have $rules and $fields too...

So what I was thinking is to loop over "any existing" input fields and add a $rule and $fieldname to those fields...

I bet that's possible, but I'm rather new at this. Could someone point me in the right direction here?

Thanks a million already!


here come's the code:


$this->load->helper(array('form', 'url'));
$this->load->library('validation');
//set validation rules
$rules['poll_choice1'] = "trim|required";
$rules['poll_choice2'] = "trim|required";
$rules['poll_choice3'] = "trim";
$rules['poll_choice4'] = "trim";
$rules['poll_choice5'] = "trim";
$rules['poll_choice6'] = "trim";
$rules['poll_choice7'] = "trim";
$rules['poll_choice8'] = "trim";
$rules['poll_choice9'] = "trim";
$rules['poll_choice10'] = "trim";
$this->validation->set_rules($rules);

//set default names for display in error message
$fields['poll_choice1'] = 'This option should at least be filled in';
$fields['poll_choice2'] = 'This option should at least be filled in';
$fields['poll_choice3'] = 'Option 3';
$fields['poll_choice4'] = 'Option 4';
$fields['poll_choice5'] = 'Option 5';
$fields['poll_choice6'] = 'Option 6';
$fields['poll_choice7'] = 'Option 7';
$fields['poll_choice8'] = 'Option 8';
$fields['poll_choice9'] = 'Option 9';
$fields['poll_choice10'] = 'Option 10';
$this->validation->set_fields($fields);




Theme © iAndrew 2016 - Forum software by © MyBB