Welcome Guest, Not a member yet? Register   Sign In
Can't get set_value to work with array fields
#1

[eluser]Unknown[/eluser]
Hi all,

I've been racking my brain over the following problem. I've got a form with arrays as field names but I can't get set_value to repopulate them properly. I've been trying to fix the problem myself and searching the forums for a relevant post, but I couldn't find one, so I'm going to post it myself. I've recreated the problem in the simplest of examples, namely the Form Validation tutorial in the CodeIgniter Manual. As I understand it, using array fields should work like this:

Code:
<input type="text" name="colors[]" value="<?php echo set_value('colors[]'); ?>" size="50" />

But it doesn't. I tried the following code (basically just adding square brackets to the username field) and this returns the value "Array" in the username field if the form does not validate.

NB: In the real world code I use jQuery to dynamically add and remove form fields from a list and it is for this reason that I'd like to work with arrays. For the same reason I don't want to explicitly number the fields.

How can I get this to work? Any help would be greatly appreciated.


Here's the code I used in this example (I'm using the template engine):
(I've not included the viewfile for the succes page)

View myform.php:

Code:
<?php echo validation_errors(); ?>

<?php echo form_open('form'); ?>

<h5>Username</h5>
&lt;input type="text" name="username[]" value="&lt;?php echo set_value('username[]'); ?&gt;" size="50" /&gt;

<h5>Password</h5>
&lt;input type="text" name="password" value="&lt;?php echo set_value('password'); ?&gt;" size="50" /&gt;

<h5>Password Confirm</h5>
&lt;input type="text" name="passconf" value="&lt;?php echo set_value('passconf'); ?&gt;" size="50" /&gt;

<h5>Email Address</h5>
&lt;input type="text" name="email" value="&lt;?php echo set_value('email'); ?&gt;" size="50" /&gt;

<div>&lt;input type="submit" value="Submit" /&gt;&lt;/div>

&lt;/form&gt;


Controller form.php:

Code:
&lt;?php

class Form extends Controller {
    
  function index()
  {
    $this->load->helper(array('form', 'url'));
        
    $this->load->library('form_validation');
        
    $this->form_validation->set_rules('username[]', 'Username', 'required');
    $this->form_validation->set_rules('password', 'Password', 'required');
    $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
    $this->form_validation->set_rules('email', 'Email', 'required');
            
    if ($this->form_validation->run() == FALSE)
    {
      $this->template->write_view('content', 'myform');        
    }
    else
    {
      $this->template->write_view('content', 'formsucces');
    }
    $this->template->render();
  }
}
?&gt;
#2

[eluser]phazei[/eluser]
Since it's in the docs, it must have been working in some version.

Does anyone know what was the last version it worked before it broke?
#3

[eluser]powdercoat[/eluser]
I have the exact same problem. forestclown came up with a temporary solution in this thread:
http://ellislab.com/forums/viewthread/111929/




Theme © iAndrew 2016 - Forum software by © MyBB