开发者

Why doesn't set_value() work with foo[bar][x][lorem] array inputs?

开发者 https://www.devze.com 2023-03-21 14:26 出处:网络
So let\'s say i have a form where the user can add as many dogs as they want by pressing a + button. The form is like:

So let's say i have a form where the user can add as many dogs as they want by pressing a + button.

The form is like:

Dog #1 <br/>
<input name="house[dogs][][name]" value="<?=set_value('house[dogs][0][name'])?>"/>
<input name="house[dogs][][age]" value="<?=set_value('house[dogs][0][age]')?>" />

Dog #2 <br/>
<input name="house[dogs][][name]" value="<?=set_value('house[dogs][1][name'])?>"/>
<input name="house[dogs][][age]" value="<?=set_value('house[dogs][1][age]')?>" />

On CodeIgniter, I run a form validation in order for set_value() to work as well:

$house = $this->input->post('house');
$dogs  = $house['dogs'];

$i = 0;开发者_如何学C
foreach($dogs AS $dog){
 $this->form_validation->set_rules("house[dogs][$i][name]", 'Dog Name', 'required');
 $this->form_validation->set_rules("house[dogs][$i][age]" , 'Dog Age' , 'required');
 $i++;
}

This whole thing doesn't work, How to make set_value() support array inputs like that?

Thanks in advance.


You might have to make the input name the exact same as the first parameter of set_value().

One might not be able to be [], while the other can use [0].


Very related: http://codeigniter.com/forums/viewthread/179581/ Ironically, a post I made months ago that was bumped this morning.

Also related: CodeIgniter: Validate form with multidimensional POST data


<ignore>

To make a long story short, Codeigniter does not handle indexed field names very well by default.

To simply repopulate the input and work around set_value()'s shortcomings, you can try something like this:

<?php
$value = isset($_POST['house']['dogs'][1]['age']) ?     // was the value posted?
         form_prep($_POST['house']['dogs'][1]['age']) : // if so, clean it
         '';                                            // if not, leave empty
?>
<input name="house[dogs][1][age]" value="<?php echo $value; ?>" />

Since you're probably using a loop to output these, I don't think it will be too much of a bother. You could populate a separate array of values and read those instead if you wish, you get the idea. set_value() automatically runs form_prep(), so that's why I added it.

I'm not too sure about the validation. You may have to do the validation yourself, which while bothersome, shouldn't be too difficult. Remember you can always run the validation methods manually. Example:

if ($this->form_validation->valid_email($this->input->post('email')) {}

You may want to just take the easy way out and change your field names to use a single index, like dog_age[], which I believe will make the validation easier for CI to handle. Best of luck, hoping for a fix one of these days in CI core.

</ignore>


EDIT: I have no idea how this escaped me, but apparently validation and set_value should in fact work as expected - not sure if this was a recent change or if the issue never really existed. I definitely remember having issues with it before, and the linked posts suggests others are too. Check out this answer though:

CodeIgniter: Validate form with multidimensional POST data

I tested it (running 2.0.2) and it does in fact work. I don't see anything in the change log, but I did test it and it did work. Make sure your on the latest version and try again perhaps, or let us know if I'm missing something here.

Like your other answer says, you probably just have to explicitly index the field names like name="house[dogs][1][name]" instead of name="house[dogs][][name]".

0

精彩评论

暂无评论...
验证码 换一张
取 消