I have a form in which public users enter in radio buttons.
These radio buttons purposefully have no field selected so that we can guarantee that a user either did or did not select this.
When the fields are required an error is presented when none are selected.
This works properly.
Now, I have conditional_fields module installed.
This is necessary to make options available and not available depending on what the user selects or does not select.
Some of these fields are "conditionally required" fields.
If the controlling field is set to an option that enables a conditionally required field, then that field must be required by the form.
The problem I am having is that when a conditionally required field is not being required because the condition is not met, I get an "An illegal choice has been detected" error.
This happens because the radio button has not been set to anything (and should not be).
In includes/form.inc around line 698, the code <?php if (isset($elements['#options']) && isset($elements['#value'])) { ?> does not allow for an empty string. An empty string happens when a field is unset.
What happens is that the field is generated as required, but no longer becomes required through conditional_fields changes.
As a result the "N/A" does not appear (and should not appear). on the form.
When the "N/A" is not available for radio buttons (or the "None" not available for select lists), then an illegal choice has been detected error is improperly thrown.
I suggest a change like the following:
Change From
<?php
if (isset($elements['#options']) && isset($elements['#value'])) {
?>to
<?php
if (isset($elements['#options']) && isset($elements['#value']) && (!isset($elements['#required']) && !(!count($elements['#value']) || (is_string($elements['#value']) && strlen(trim($elements['#value'])) == 0)))) {
?>| Comment | File | Size | Author |
|---|---|---|---|
| #13 | issue_919348-2.patch | 712 bytes | thekevinday |
| #4 | 919348-unset-form-values.patch | 850 bytes | damien tournoud |
| #3 | 919348_3-D6.patch | 1.1 KB | scottrigby |
| #3 | 919348_3.patch | 1.09 KB | scottrigby |
Comments
Comment #2
miklThis is also an issue for Drupal 7.x. This example form illustrates the problem:
This form, if submitted without any filling any of the fields, yields a “An illegal choice has been detected. Please contact the site administrator.” in addition to the “Data file field is required.”. The appropriate error would be “Data file type field is required.”
Comment #3
scottrigby@thekevinday: adding patch against HEAD.
also adding patch for d6 latest dev, to help #453204: Validation error produced by empty required conditional fields (radio buttons, checkboxes) for example.
Comment #4
damien tournoud commentedI wonder what this could break.
Comment #5
pivica commentedPatch from #4 solved another problem I have with http://drupal.org/node/1081196#comment-4170278 (using Drupal 7.0).
I have also done test with custom form with required radio buttons but without default value.
If I submit this form without selecting some radio option I will get 'An illegal choice has been detected' error. With patch from #4 error will be correct one 'Radios test field is required.'.
Comment #6
steven jones commentedSubscribe.
Comment #7
Isostar commentedsubscribe
Comment #8
kbk commentedcan anyone confirm that this patch works for D6.21 and D6.22, please?
Comment #9
thekevinday commentedComment #10
itserich commentedRegarding the patch for the 6 version in #3 I think, could someone please explain how to apply it manually?
I have applied a few patches but can not figure out how to apply this through command line, and it looks manageable to apply manually.
Thank you.
Comment #11
drupalnesia commentedAbove is patch #3 (http://drupal.org/files/issues/919348_3.patch)
above mean the code around line 1167
above mean you must delete this line
above mean you must add this codes.
IMPORTANT: please make a copy before edit file!!!
Comment #12
itserich commentedThank you drupal-id
My cpanel is having trouble saving modified files today for an unrelated reason so have not been able to try it yet, but thank you for the quick response.
Looks like for version 6 it is just replacing one line. Does this modify the Drupal core? I do not think I have done that before.
Comment #13
thekevinday commentedThe original fixes I mentioned were for D6, so I was recently testing this in D7 now that it has come back to my attention.
With Drupal 7 "An illegal choice" message is displayed instead of the more user-friendly "Field is required..." message.
It seems that D7 already does the checks (and possibly D6) but at a later point in time.
So why was the illegal choice happening?
The reason seems to be that instead of an array, a boolean value of FALSE is stored inside of
$elements['value'].How exactly was a boolean value being stored when a string, array, or null value should be present?
Further research showed that in modules/field/modules/options/options.module:105
the php function reset() was being called.
When the reset function gets invalid data, it returns FALSE.
The attached patch changes the behavior to return an empty array if $default_value is an empty array, instead of FALSE.
Note: I tried to return NULL, but it seems if I use the actual field whose data type is expected to be a boolean, NULL ends up getting treated as FALSE.
I have not been able to test conditional fields in D7 to see if this fixes the bug there.
As far as #4 above, the patch is completely different than the changes I suggested. Why? In what way does this patch solve the problem?
Comment #14
hnln commentedsub
Comment #15
itserich commentedI think there might be a conflct with conditional 6 3. dev and editable fields 6 3. dev, so I am going to stick with conditional6 2. beta for now.
Comment #16
thebuckst0p commentedI'm encountering the same bug, Drupal 7.2, not using Conditional Fields. When I try to "add another item" in a repeating text field, if a separate required radio field is not selected, it throws an "Illegal" error. Is the patch in #13 stable and going to be committed?
Thanks thekevinday for doing all that legwork!
Comment #17
dmadruga commentedThe patch in #13 solved my problem. Tks!
Comment #18
michelleThis looks like a duplicate of #811542: Regression: Required radios throw illegal choice error when none selected which is seeing lots of core dev action on it.
Michelle
Comment #19
robertom commentedSubscribe
Comment #20
DWB Internet commented#4: 919348-unset-form-values.patch queued for re-testing.
Comment #21
chris.smith commentedWe had the same issue, but the error was displayed when we click on an "Add more" button for a different form element.
Patch #13 resolves the issue for us.
Comment #22
alibama commented#3: 919348_3-D6.patch queued for re-testing.
Comment #23
tim.plunkett#811542-179: Regression: Required radios throw illegal choice error when none selected has a better fix for this.