CodeIgniter icon indicating copy to clipboard operation
CodeIgniter copied to clipboard

Multi-dimensional fields in set_radio/set_checkbox

Open aanbar opened this issue 13 years ago • 13 comments

The form_helper should relay on CI's input->post() method instead of using $_POST array for setting things just like set_value is doing right now.

Currently trying to set things like options['label']['something'] will fail as it cannot be translated using the $_POST directly.

aanbar avatar Sep 12 '13 15:09 aanbar

Could you give an example of what fails?

narfbg avatar Sep 12 '13 17:09 narfbg

Re-Producing the issue isn't easy, it happens when $OBJ === FALSE (when you don't have validation loaded) OR when you have validation loaded & you're not validating the field in question.

You may test it however you want by supplying checkboxes or radios

e.g: for checkbox name => 'options[type][check][]' e.g: for radio name => 'options[type][other]'

This can be fixed by using $CI->input->post in the form_helper, but I have no idea on how to get it fixed for the form_validation as you're only loading data for the fields that are being validated.

aanbar avatar Sep 12 '13 19:09 aanbar

It's not even necessarily POST data if you're using the library, so don't worry about that.

narfbg avatar Sep 12 '13 21:09 narfbg

I guess a fix for this would also requires changing '_field_data' from protected to public in the form_validation so you can check if there are field data, a rule is set so form_validation would take care of it, otherwise the form_helper will take care of it.

something like

if ($OBJ === FALSE OR ! isset($OBJ->_field_data[$field]) )
// let the form_helper handler it

aanbar avatar Sep 12 '13 21:09 aanbar

No, the library stays as it is. If you'll be using it - you should have rules for all fields.

narfbg avatar Sep 12 '13 22:09 narfbg

That wasn't nice, Anyway after Looking again at the helper, looks like $OBJ->has_rule($field) can be used so no modification would be needed to the validation library.

The current way shown there isn't following the standards applied in other setters, Look at set_value for example, it works with or without form_validation.

Currently if you auto load form_validation, You'll be rendering half (if not most) of the setters functions useless when not using validation for the given field which makes no sense at all (having validation class loaded doesn't mean the setter should go there unless there are rules defined for the given field).

aanbar avatar Sep 12 '13 23:09 aanbar

Yes, has_rule() is applicable. In fact, it was introduced for that same purpose - look at set_value(). :)

narfbg avatar Sep 13 '13 09:09 narfbg

How about now?

narfbg avatar Sep 13 '13 13:09 narfbg

Close, but for checkbox the last return should be changed to make it work.

return ( ( is_array($input) && in_array($value, $input, TRUE) ) OR ( $input === $value) ) ? ' checked="checked"' : '';

aanbar avatar Sep 13 '13 15:09 aanbar

Just noticed, there's a typo in set_select where it says "checked=selected" instead of "selected=selected"

And this one too requires the same changes I mentioned in my previous comment, as you may supply multiple=multiple when you make a dropdown menu.

aanbar avatar Sep 13 '13 15:09 aanbar

Anything else?

narfbg avatar Sep 13 '13 15:09 narfbg

Looks perfect now, Thank you.

aanbar avatar Sep 13 '13 15:09 aanbar

You're welcome. :)

narfbg avatar Sep 13 '13 16:09 narfbg