简体   繁体   中英

Codeigniter - Form validation converting post variables from Array to “Array” string

I am building an HTML form with a set of checkboxes for selecting multiple categories using this format:

<input type="checkbox" name="category[]" />

So, when I post and print_r($_POST) to view the variable and values I get:

Array
(
    [27] => on
    [28] => on
    [29] => on
)

Once I run $this->form_validation->run(); the categories array becomes "Array" as a string. I believe I have narrowed it down to "prep_for_form" function in the system/libraries/Form_validation.php file, but it seems like the recursive function is working correctly.

Thank you in advance.

I figured it out. When using the $this->form_validation->set_rules() method, in the validation rules (third parameter) I set it to trim|required . I guess the trim function treats the actual Array like a string "Array" and trims the word. I simply removed the "trim" rule from my validation rules. eg

$this->form_validation->set_rules('category', 'categories', 'trim|required');

// when parsing an array (set of checkboxes, radio buttons, etc.) 
// - remove the "trim" validation
$this->form_validation->set_rules('category', 'categories', 'required');

I hope others find this useful.

该解决方案对我不起作用,但是这里列出了一个修复程序,该修复程序确实有效: http : //ellislab.com/forums/viewthread/156497/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM