Welcome Guest, Not a member yet? Register   Sign In
Multidimensional form arrays in Code Igniter with form_input and set_value
#1

[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:
&lt;?=form_open(base_url().'add/fixes');?&gt;
      
                <fieldset>
          <legend><h2>Add Fixes</h2></legend>
            
          &lt;? for($i=0;$i<$num;$i++): ?&gt;
          
          <div class="duplicates duplicate_&lt;?=$i?&gt;">
              <h4>Fix #&lt;?=$i?&gt;</h4>
            <div class="form_row">
              <div class="form_field">
                <label for="name_field">Name of Fix</label>
              </div>
              <div class="form_field">
                &lt;?
                //form_input("name_field[]",set_value("name_field[]"));
                                ?&gt;
                &lt;input type="text" name="name_field[]" value="&lt;?=set_value("name_field[($i+1)]")?&gt;" /&gt;
                &lt;?=form_error("name_field[($i+1)]");?&gt;
              </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">
                &lt;?=form_textarea('url_field[]',set_value('url_field[]',"http://"),'class="content"');?&gt;
                &lt;?=form_error('url_field[]');?&gt;
              </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">
                &lt;?=form_textarea('desc_field[]',set_value('desc_field[]'),'class="content"');?&gt;
                &lt;?=form_error('desc_field[]');?&gt;
              </div>
            </div>
                        
                    </div>
          &lt;? endfor; ?&gt;
                    
          <div class="form_row">
            <div class="form_field">
                  &lt;?=form_button('add_more','Add Another Fix','id="add_more"');?&gt;
            </div>
          </div>
          
                    
          <div class="form_row">
            <div class="form_field">
                &lt;?=form_hidden('num',set_value('num',1),'id="num"');?&gt;
                  &lt;?=form_submit('submit','Next Step >');?&gt;
            </div>
          </div>
          
        </fieldset>
        
        &lt;?=form_close();?&gt;
Could not post full script because of char limit.


Messages In This Thread
Multidimensional form arrays in Code Igniter with form_input and set_value - by El Forum - 09-13-2010, 09:00 AM



Theme © iAndrew 2016 - Forum software by © MyBB