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).

Comments

anavarre’s picture

I'm also interested in good practice for this !

litzastark’s picture

Category: support » bug

Hmmm... 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."?

quicksketch’s picture

Hm, 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?

anavarre’s picture

@quicksketch - Sounds excellent to me !

tevans’s picture

+1

sibert’s picture

Version: 7.x-3.8 » 7.x-3.11

If I understand correct, there is no solution yet? Or workaround?

cwightrun’s picture

^ I'm currently still experiencing the same lack of "Please Select".

+1

sibert’s picture

How 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?

quicksketch’s picture

When did this "Please Select..." disappear? Any problem with downgrading?

AFAIK, 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.

quicksketch’s picture

Status: Active » Fixed
StatusFileSize
new661 bytes

So 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.

quicksketch’s picture

Title: How to create an empty default option in non-required select lists » Options for "- Select -" or "- None -" not automatically added when no default is specified
sibert’s picture

Component: Code » User interface
Assigned: Unassigned » sibert
Category: bug » support
Priority: Normal » Minor
Status: Fixed » Active

Thanks! "webform_select_none.patch" works excellent! Where do I translate the - Select - to swedish?

TIA

richardbporter’s picture

Works perfectly. Thank you.

quicksketch’s picture

Assigned: sibert » Unassigned
Category: support » bug
Priority: Minor » Normal
Status: Active » Fixed

Please 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.

David_Rothstein’s picture

Title: Options for "- Select -" or "- None -" not automatically added when no default is specified » Options for "- Select -" (regression) or "- None -" not automatically added when no default is specified
Status: Fixed » Needs review
StatusFileSize
new4.68 KB

Reopening 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:

  • For non-mandatory selects with no default value, the "-None-" option appears and works correctly.
  • For mandatory selects with no default value, you can't submit the form without choosing something (i.e., the "-Select-" option works correctly). These tests also test the same behavior for other mandatory components with no default value too.
  • The bug in #1092756: Unable to not have a default option for mandatory radio button group with a key of 0 does not reoccur (since I was tempted to propose rolling that patch back before I realized that the patch here already fixes the regression in a different way).

Hope this is helpful.

m.e.’s picture

What's the status of getting the fix committed? I'm also running into this problem in 3.11.

anou’s picture

Hello,
This patch from #15 David Rothstein, work like a charm for me. Thanks a lot.

BernieCram’s picture

Hi 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

Screenack’s picture

patch at #10 scratched my itch; thanks!

j-3p0’s picture

where do I apply this patch, what file and what line?

Thanks from a n00b ;)

quicksketch’s picture

quicksketch’s picture

Status: Needs review » Fixed
StatusFileSize
new6.45 KB
new5.79 KB

Thanks 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.

colemanw’s picture

Status: Fixed » Needs review

Quicksketch, 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.

quicksketch’s picture

Quicksketch, 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.

Hmm, isn't my patch identical to David's in #15, except additional fixes to the tests?

colemanw’s picture

Title: Options for "- Select -" (regression) or "- None -" not automatically added when no default is specified » Options for "- Select -" or "- None -" not automatically added
StatusFileSize
new691 bytes

Yes, 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."

quicksketch’s picture

Ah I see. Okay I'll take another look at this issue and re-test.

David_Rothstein’s picture

Technically 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? :)

quicksketch’s picture

Yeah 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.

quicksketch’s picture

Status: Needs review » Fixed

Let'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.

colemanw’s picture

quicksketch: 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.

David_Rothstein’s picture

colemanw: 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.

quicksketch’s picture

I agree with David here again. What might be helpful would be if you could specify an option without a key at all, such as:

|None

But we'd have to get that working with Options Element module as well.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Antoine Lafontaine’s picture

Status: Closed (fixed) » Active

Sorry 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.

quicksketch’s picture

Webform already makes an option for "- Select -". Is the only difference that you want it to say "make a choice" instead?

vdchristelle’s picture

colemanw’s picture

It is working in the current version of Webform. You probably just need to remove the default value for your field to see - Select -

quicksketch’s picture

Status: Active » Closed (fixed)

There 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.