Welcome Guest, Not a member yet? Register   Sign In
dynamic inputs and validation
#1

[eluser]robert.fulcher[/eluser]
I am trying to validate and repopulate fileds on a form that I also add dynamically. Not dynamically from JavaScript but from database variables.

I have a preferred number of fields that will be added to a form.

Form HTML

Code:
<?php echo form_input('value['.$howmany.']', set_value('value['.$howmany.']')); ?> <?php echo form_error('value['.$howmany.']'); ?>

Generated HTML

Code:
<input type="text" name="value[1]" value=""  />
<input type="text" name="value[2]" value=""  />
<input type="text" name="value[3]" value=""  />
<input type="text" name="value[4]" value=""  />

Controller

Code:
$this->form_validation->set_rules('value[]','Communication Value');

I read about some posts that addressed the index value in the array with patches and things. I am still new to CI so I am not sure if this is supposed to be working or not. The re-population does not work.

Any help would be great. Thanks
#2

[eluser]robert.fulcher[/eluser]
Still looking for input on this if anyone has it. Am I even close with how this should work?

Thanks
#3

[eluser]LuckyFella73[/eluser]
Hi Robert,

did you try to set up the input fields this way ?
Code:
<input type="text" name="value[]" value=""  />
<input type="text" name="value[]" value=""  />
<input type="text" name="value[]" value=""  />
<input type="text" name="value[]" value=""  />

I didn'd test but looking at the user guide it seems to be the direction to go.

Have a look here:
http://ellislab.com/codeigniter/user-gui...ysasfields
#4

[eluser]xwero[/eluser]
Normally it should work to validate an array but as the dynamic fields come out of a database why not do
Code:
<?php echo form_input('value_'.$howmany, set_value('value_'.$howmany)); ?> <?php echo form_error('value_'.$howmany); ?>
And in the controller
Code:
for($i=1;$1<=$data['howmany'];$i++)
{
   $this->form_validation->set_rules('value_'.$i,'Communication Value '.$i,'required');
}
Array validation is still a bit clunky even with the new validation class.




Theme © iAndrew 2016 - Forum software by © MyBB