In form.inc, form_type_checkbox_value, line 2195, 0 is returned instead of NULL for a checkbox. This creates a problem when trying to execute a valid form_state that contains an element of type 'checkboxes'. An error will occur on forms that use this element, for example the 'block_admin_configure' form.
I have saved a previously submitted form and try to submit it using drupal_form_submit later on. This form_state has input, values, etc. The complete thing. I'd say that this must be possible.
But drupal_form_submit simply overwrites the input array with the form_state['values'] array. Needless to say, this is only sensible when their formats are identical. In the case of a checkboxes-element, the net result is that 'NULL' values (not-selected checkboxes) in the input field will be overwritten by the value '0'. This is asking for trouble.
In the function 'form_type_checkboxes_value' (form.inc, line 2211) the '0' value is not recognized as a 'not set' value (check the comment block!). isset is used, and as the value is not NULL, it thinks that the 0 value is actually a chosen option and the value is taken into the '#value' array key of the element.
The trouble happens later in the function _form_validate (form.inc, line 1249), where the value 0 is not recognized as a correct 'option', and the error 'An illegal choice has been detected. Please contact the site administrator.' is set. The form will not be accepted.
My solution is to re-implement drupal_form_submit locally, but of course this is not a good solution. Some more intelligence is required in drupal_form_submit, so that form_state['values'] are correctly converted to input values. Personally, I think that the input and form_state['values'] should always have the exact same format if the input array is overwritten.
Comment | File | Size | Author |
---|---|---|---|
#10 | incorrect_checkbox_value_in_form_state-1195306-10.patch | 1.56 KB | Keshav Patel |
#8 | incorrect_checkbox_value_in_form_state-1195306-8.patch | 1.58 KB | HitchShock |
Issue fork drupal-1195306
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #1
Anonymous (not verified) CreditAttribution: Anonymous commentedI think that your issue is related to my post here: http://drupal.org/node/1161024 . Basically, reusing the $form_state from a form_submit function should work with drupal_form_submit, but with certain Fields, it doesn't. Something needs to be done to get this fixed in Core.
Sincerely,
Leighton Whiting
Comment #2
SilviaT CreditAttribution: SilviaT commentedsub
Comment #3
Ben Young CreditAttribution: Ben Young commentedI understand that this isn't likely to receive much attention due to being able to handle things via e.g. node_save() or another alternative method, but I've been struggling against this bug for hours because nowhere does it state that drupal_form_submit() shouldn't just work.
The documentation even includes an example for creating a user. If there were select fields involved, it would fail.
Comment #4
rudolfbykerIs there any progress on this? I am working on some advanced forms, which do not work because of this bug. Re-implementing drupal_form_submit is not an option for me... Does this also happen in 7.23?
Comment #5
donquixote CreditAttribution: donquixote as a volunteer commentedI am running into the same issue.
My use case is quite advanced so I hesitate trying to explain it here.
It has to do with views_ui and ajax (D7).
Comment #7
HitchShockI reproduced this issue too.
We can reproduce it only when using drupal_form_submit() programmatically. In this case we have to use the input value instead of #return_value because we are setting new values via form_state. Like this:
See form_type_checkbox_value() and _form_builder_handle_input_element:2119.
I prepared a simple patch to fix it.
Comment #8
HitchShockUpdated the patch. We need to skip cases when checkbox is the entity field.
Comment #9
apadernoThe latest patches do not apply anymore.
Comment #10
Keshav Patel CreditAttribution: Keshav Patel at OpenSense Labs for DrupalFit commentedUpdated the #8 patch, the patch on #10 applies cleanly. please review.
Comment #12
apadernoI created a MR with the same changes in the patch in comment #7.
Comment #13
apaderno[Please ignore this comment.]
Comment #14
apadernoThe description of the return value needs to be changed too.
form_type_checkbox_value()
always return a value, even if that value is 0. It is not true it can return nothing. That comment cannot be changed to or 0 to use the default either. That would contrast with the comment insideform_type_checkbox_value()
, which contains the following sentence.For a checkbox, 0 returned from
form_type_checkbox_value()
means the checkbox is unchecked.Furthermore, this comment should be changed too, since the code is changed.
we return the original #return_value is not always true, as
$input
is returned for programmatically submitted forms.Comment #15
apadernoI did not change the other phrase.
Given the context in which it is used (browsers submit the string version of
#return_value
, but we return the original#return_value
), it is not necessary to say that also$input
could be returned.