Forgive me if this is a duplicate, but I've searched the issues for this problem and though #885454: First item of select lists selected as default if no default is set is similar, it's been closed for a long while (#1009168: Webform has a redundant implementation for mandatory select lists with no default value seems related as well). I'm not sure if this should be a support request or a bug report; I'm afraid I must be missing something obvious since I don't see anyone else describing the problem!
In a nutshell: what's the accepted way to create an empty default option in a non-required listbox?
Specific example:
I'm trying to create a non-required select element with the following choices (and no default value):
Sr|Sr.
Jr|Jr.
II|II
III|III
IV|IV
On the form, this element appears with "Sr." already selected and no way to select a blank value. I've tried working around the problem by adding an initial option with 0 as the key and either an empty string or "- Select -" as the value, but the empty string is confusing when the form is displayed and "- Select -" is confusing as a submitted value.
If I set the element as required, "- Select -" appears as one of the options. It seems to me that even if a select list element is not required, an empty key with "- Select -" as the displayed value should be automatically used as the first item in the list (or at the very least, that this should be an option when creating the form).
| Comment | File | Size | Author |
|---|---|---|---|
| #25 | webform_select_none.patch | 691 bytes | colemanw |
| #22 | webform_select_empty-d6.patch | 5.79 KB | quicksketch |
| #22 | webform_select_empty-d7.patch | 6.45 KB | quicksketch |
| #15 | select-fix-plus-tests-1078750-15.patch | 4.68 KB | David_Rothstein |
| #10 | webform_select_none.patch | 661 bytes | quicksketch |
Comments
Comment #1
anavarreI'm also interested in good practice for this !
Comment #2
litzastark commentedHmmm... can there really only be two people affected by this? Does anyone have a good workaround or solution so that all the people submitting my form don't have to be "Sr." or "Jr."?
Comment #3
quicksketchHm, yeah definitely the default behavior is not very good. In Drupal 6 we actually used to add the "- Select -" option when the field *wasn't* required, the opposite of how it is now. In Drupal 7, we removed this code because Drupal 7 was supposed to provide the option for us and the old code was assumed to be unnecessary. Clearly not the case here though. Seems like it should be added in both situations (field required or not required) any time a default is not provided for a select list. Sound good to you guys?
Comment #4
anavarre@quicksketch - Sounds excellent to me !
Comment #5
tevans commented+1
Comment #6
sibert commentedIf I understand correct, there is no solution yet? Or workaround?
Comment #7
cwightrun commented^ I'm currently still experiencing the same lack of "Please Select".
+1
Comment #8
sibert commentedHow about downgrading to a previous version of webform module. I am using Webform 7.x-3.11 and Drupal core 7.2
When did this "Please Select..." disappear? Any problem with downgrading?
Comment #9
quicksketchAFAIK, it has never existed in a stable release of Webform for Drupal 7. I think it was removed some time in the early betas for Drupal 7, so I wouldn't recommend downgrading to attempt to get back the functionality. Fixing the problem and upgrading is a much better route.
Comment #10
quicksketchSo the Drupal 6 version of the module manually adds "- Select -" (for required fields) and "- None -" for fields that are optional without a default. These seem like reasonable settings to me as it's what Drupal 7 should be doing for us automatically anyway. The thing I didn't realize before was that we need to manually trigger this behavior in order to generate the empty option. This patch adds a work-around in the same place as the Drupal 6 version of Webform. I've committed this patch to the D7 branch only, as the D6 version works as expected.
Comment #11
quicksketchComment #12
sibert commentedThanks! "webform_select_none.patch" works excellent! Where do I translate the - Select - to swedish?
TIA
Comment #13
richardbporter commentedWorks perfectly. Thank you.
Comment #14
quicksketchPlease do not change "bug reports" to support requests. Keeping it in the right category makes it easier for me to find later. You translate "- Select -" and "- None -" in the Locale module interface just like any other string. Since these strings are part of Drupal core, they should be translated for you already.
Comment #15
David_Rothstein commentedReopening because the bug still occurs... It looks like the above patch was never committed.
Also, although the lack of a "-None-" (for non-mandatory select components) does appear to be a longstanding bug, the lack of a "-Select-" (for mandatory ones) is actually a recent regression. The form API in Drupal 7 will add that in correctly, and it used to work in Webform too, but it looks like the patch committed in #1092756: Unable to not have a default option for mandatory radio button group with a key of 0 caused that to regress. In any case, the patch in this issue does fix both cases.
Since I just spent more time thinking about this than I ever want to, I decided to write some tests so that hopefully no one has to think about it again :) The attached patch is the same as #10, but adds tests which verify that:
Hope this is helpful.
Comment #16
m.e. commentedWhat's the status of getting the fix committed? I'm also running into this problem in 3.11.
Comment #17
anouHello,
This patch from #15 David Rothstein, work like a charm for me. Thanks a lot.
Comment #18
BernieCram commentedHi I have applied the patch at #10 and it works as expected. I now have -select- for mandatory fields and -none- for non-required ones. I did this the other day before you had posted @David_Rothstein. Just commenting incase that helps to get it applied.
Thanks quicksketch and David_Rothstein
B
Comment #19
Screenack commentedpatch at #10 scratched my itch; thanks!
Comment #20
j-3p0 commentedwhere do I apply this patch, what file and what line?
Thanks from a n00b ;)
Comment #21
quicksketchhttp://drupal.org/patch/apply
Comment #22
quicksketchThanks again David!
I tried this patch out and tried running the tests to confirm that the new ones work as expected. Unfortunately #301949: Add option to only allow dates in the past/future caused a break in the tests, which I've rolled into these patches so that everything is working again. Thanks guys.
Comment #23
colemanw commentedQuicksketch, your patch adds a null option to all select lists except those with a default value. However, non-required elements should allow the user to select "none" even if a default is set.
Comment #24
quicksketchHmm, isn't my patch identical to David's in #15, except additional fixes to the tests?
Comment #25
colemanw commentedYes, his patch did not address that problem either.
This very minor change to the #10 patch gives the behavior of always allowing "- None -" to be selected for non-required fields.
This adheres to the advise given in the FAPI reference page, to avoid a situation in which "there is no way for the user to select nothing, and the user is also not forced to make an active decision."
Comment #26
quicksketchAh I see. Okay I'll take another look at this issue and re-test.
Comment #27
David_Rothstein commentedTechnically that might be the correct behavior, but in practice I'm not sure it's a good idea.
What it would mean in practice is that for every single select list, I have to choose between having a random "None" option at the beginning of the list (which I may not want) or marking the field 'officially' required in order to make that None option go away (which is extra work for me as the site admin, and which also means Drupal sticks a nice big red asterisk next to the field label, which I may not want either). From a usability standpoint, I'm not sure people would even understand how to get rid of that phantom "None" option, either.
Also, if we do this for select lists, the same arguments apply for radios too, right? Where does it stop? :)
Comment #28
quicksketchYeah after testing and having another look, I think that the current implementation is the expected behavior. "- None -" is already added if no default value is selected by default. In the event that a user wanted to get rid of the "- None -" or "- Select -" options, I think that it's a good option to just specify a default to make them go away.
Comment #29
quicksketchLet's start a new request if it's considered desirable behavior to always add a "- None -" option even on fields that have a default value. I personally think the current behavior is preferable, but in any case throwing 3 rounds of patches in the same issue is not a good practice.
Comment #30
colemanw commentedquicksketch: I think the best solution may be to give the user control over this, including the ability to set the label. I have in mind how this could be done (add the control to $field['extra']) and will open an issue and submit a patch if you would be willing to review it.
Comment #31
David_Rothstein commentedcolemanw: Doesn't the user already have control over this? If you want one of the options in your select list to be "None" (or similar), you can just create one manually with that label.
Comment #32
quicksketchI agree with David here again. What might be helpful would be if you could specify an option without a key at all, such as:
But we'd have to get that working with Options Element module as well.
Comment #34
Antoine Lafontaine commentedSorry to reopen this, but I have a use case I'd like to add to this issue.
I have a select list of option where the user needs to make a choice (mandatory field) but is provided a default field of "make a choice". So that we make sure the user doesn't submits the form without making a proper choice (and not submit the default field which is "make a choice").
This requires a custom validation to make this happen with how webform handles validation on required selection list... Maybe I need to dig deeper and see if there's a key that, when set on an option would make webform fail validation on submit, but this is a valid use case I think should be supported somehow.
Comment #35
quicksketchWebform already makes an option for "- Select -". Is the only difference that you want it to say "make a choice" instead?
Comment #36
vdchristelle commentedComment #37
colemanw commentedIt is working in the current version of Webform. You probably just need to remove the default value for your field to see - Select -
Comment #38
quicksketchThere is a new feature request for custom empty text here: #1331956: Allow select components (list, single-select, no default) to customize name of empty list option. Please keep this issue closed in the future and file a new report instead.