Welcome Guest, Not a member yet? Register   Sign In
Form Validation: Using Arrays as Field Names in 2.0

I'm upgrading an existing application from 1.7.x to 2.0, and I see that the behavior of form validation field arrays has changed. Unfortunately, the new documentation doesn't really show how to process multiple values in your controller.

For example, my form allows the user to add new input fields via Javascript. In other words, there could be any number of these:

<input type="text" name="editor[]" value="" />

In the old days, submitting the form and calling $this->form_validation->set_value('alias[]') would give you an array you could loop through. Now, it seems set_value keeps a count and will only give you one value at a time. In my controller, how can I get all of the post values for editor[]?

I guess I could do something like this, but it's inelegant, and it doesn't account for a user who leaves a middle field blank:

do {
    $new_value = $this->form_validation->set_value('editor[]');
    if ($new_value != '') {
        $editors[] = $new_value;
} while ($new_value != '');

I know I could also use $this->input->post('editor[]'), but what if I want to populate an edit form? Before, I could do this in my controller:

$data['editors'] = $this->form_validation->set_value('editor[]', $saved_editors);

Then I would loop through the values in my view. ($saved_editors comes from my database.)

I have the same problem. I woulud use the do..while way to replace my way that use function loop. Thanks.

It ended up being too much trouble to replace clean code like this with a bunch of loops:

$data['aliases'] = $this->form_validation->set_value('alias[]', $saved_aliases);
$data['parents'] = $this->form_validation->set_value('parent[]', $saved_parents);
$data['number_names'] = $this->form_validation->set_value('number_name[]', $saved_number_names);
$data['number_phones'] = $this->form_validation->set_value('number_phone[]', $saved_number_phones);
$data['editors'] = $this->form_validation->set_value('editor[]', $saved_editors);

So I ended up just creating a MY_Form_validation extending the CI one and overriding set_value() with the old logic.

Theme © iAndrew 2016 - Forum software by © MyBB