[eluser]Ninjabear[/eluser]
Hi. I've been playing around with CI for a good while but have come across a serious problem with multidimensional arrays with forms. My application contains several blocks of code, each containing a name, url and description (textarea). First time the form is run I present one group of fields. If the user hits "add" they get another set. When the form is submitted it get validated and if something is wrong I need CI to validate the fields and place the info back in if there's a validation error, nothing weird there.
But the problem is that when the form is presented a second time I get "Array" in the text box. I've tried using:
form_input("name_field[]",set_value("name_field[]"));
form_input("name_field[index]",set_value("name_field[index]"));
<input type="text" name="name_field[]" value="<?=set_value("name_field[]")?>" />
Have read the following docs:
http://ellislab.com/codeigniter/user-gui...ysasfields
http://ellislab.com/codeigniter/user-gui...elper.html
My Controller in controllers/add/fixes
Code:
function Fixes()
{
$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
$this->form_validation->set_rules('name_field[]','Name of fix','trim|required|min_length[5]|max_length[100]');
$this->form_validation->set_rules('desc_field[]','Description of fix','trim|required|min_length[10]|max_length[1000]');
$this->form_validation->set_rules('url_field[]','URLs','trim|required|callback__Check_URL_Format');
$this->form_validation->set_rules('num','','trim|required|num');
if($this->form_validation->run())
{
}
echo "post ".$this->input->post('num');
if($this->input->post('num'))//Num = number of fixes so far.
$options->num = $this->input->post('num');
else
$options->num=1;
echo "skdjksjf ".$options->num;
$this->load->view('add/step2',$options);
}
My form in views/step2
Code:
<?=form_open(base_url().'add/fixes');?>
<fieldset>
<legend><h2>Add Fixes</h2></legend>
<? for($i=0;$i<$num;$i++): ?>
<div class="duplicates duplicate_<?=$i?>">
<h4>Fix #<?=$i?></h4>
<div class="form_row">
<div class="form_field">
<label for="name_field">Name of Fix</label>
</div>
<div class="form_field">
<?
//form_input("name_field[]",set_value("name_field[]"));
?>
<input type="text" name="name_field[]" value="<?=set_value("name_field[($i+1)]")?>" />
<?=form_error("name_field[($i+1)]");?>
</div>
</div>
<div class="form_row">
<div class="form_field">
<label for="name_field">External links (URLs) to fixes (One link on each line.).</label>
</div>
<div class="form_field">
<?=form_textarea('url_field[]',set_value('url_field[]',"http://"),'class="content"');?>
<?=form_error('url_field[]');?>
</div>
</div>
<div class="form_row">
<div class="form_field">
<label for="desc_field">Describe how to perform the fix.</label>
</div>
<div class="form_field">
<?=form_textarea('desc_field[]',set_value('desc_field[]'),'class="content"');?>
<?=form_error('desc_field[]');?>
</div>
</div>
</div>
<? endfor; ?>
<div class="form_row">
<div class="form_field">
<?=form_button('add_more','Add Another Fix','id="add_more"');?>
</div>
</div>
<div class="form_row">
<div class="form_field">
<?=form_hidden('num',set_value('num',1),'id="num"');?>
<?=form_submit('submit','Next Step >');?>
</div>
</div>
</fieldset>
<?=form_close();?>
Could not post full script because of char limit.