I was wondering if there is a way to set the first option as a label of sorts, meaning the form can't be submitted unless you choose one of the options below, for example:

Choose a title
Mr | Mr
Miss | Miss
Mrs | Mrs
Dr | Dr

'Choose a title' would show as the label for the drop down list but could not be submitted as an option

The reason I want it like this is because I am using the 'Compact Forms Module' so all of my other input fields have their labels inside their fields

Cheers in advance

Comments

vernond’s picture

Maybe try <Choose a title> as a group name and set label display to None.

Tony Finlay’s picture

This only sets the default as - Select - i'm afraid.

quicksketch’s picture

Yeah, right now a label is required for each key|value pair, you can't just leave it with a |value with no key. You can set a key of some meaningless value though, such as a "-" or "n/a", but that default choice will be accepted by validation as a filled-in value, so you can't have Webform enforce a requirement.

Tony Finlay’s picture

It's not the end of the world for this current job but it would be a nice feature and I can see it being useful in future.

Perhaps someone with a bit more talent for programming than myself will need this option and do the coding.

quicksketch’s picture

Title: Select List Label » Allow blank keys in select components to provide custom "Select" or "Choose" labels

Updating the title for clarity.

alan d.’s picture

As an idea from one of my other modules, I prefixed a blank line with a double hyphen. This clearly shows out in the list, where a single hyphen can get lost.

Eg:
--Please select a Title

The help text is "Prefix a line using '--' to specify a blank value text. For example: '--Please select a Title'.".

Or the lists could be automatically populated like the core list field values. I think that this would be the easiest solution but would effect existing web-forms. Yet, this one has my vote :)

Tony Finlay’s picture

Does it really need a prefix hardcoded in there though?

I know for aesthetic reasons I wouldn't want a double-hypen in there, the text you put in there is going to be an indicator that it's not an option but a label anyway ('Please select your title', 'Please select your state' etc) so this negates any UX issues. Plus without it hardcoded you have the flexibility to add it/remove it as you please.

Plus this doesn't really solve the issue as it still requires a key and therefore would submit the 'Instructional' key as an option value as the field is filled it would validate. What's required is some programming to recognise a key named, say for argument sake, 'key-label - Please select your title' which would not validate and allow the form to be submitted.

alan d.’s picture

The prefix was just so that the default is picked up as empty. If found a key of "" is defined! :)

Eg: Example why I strayed away from specifying an empty or missing key.

|Is this an | I or 1? Depends on the users font, clear as day or impossible to tell
Just blank, or was it meant to be something. Validate an error if two empty found
--Stands out more, common key that is commonly associated with empty values

I guess adding a UI to specify the key if the best, but this has additional coding overhead.

Maybe:

Unselected additional option

[ ] Supply an empty value

JQuery toggle on selection

[*] Supply an empty value
    (*) Default values
    ( ) Custom text [-- Select a value --]
luco’s picture

I agree with Alan D.

we should have a UI to customise the the - Select - or - None - option.

I was looking for a way to customise it via TPL, theme functions or what have you, but I can't for the life of me find a way to do it.

quicksketch, if you could give me a pointer as to how can I customise the - Select - or - None - option of my select lists, I'd be eternally grateful. :)

cheers,
Luciano

ptkobe’s picture

Issue tags: +customise the - Select - or - None - option of my select lists

On Drupal 7, I'm using this on my forms and hook_form_alter():

  $form['element'] = array(
    '#type' => 'select',
    ...

then use

     '#required' => true, '#empty_value' => '', '#empty_option' => t('- Whatever Select -'),

or

     '#required' => false, '#empty_value' => '', '#empty_option' => t('- Whatever None -'),

The #empty_option for #required selects will only appears if #default_value is not set. It will not appears even if #default_value is invalid (as in '#default_value' => -1).

EDIT: Drupal 7.23:
The #empty_option for #required selects will only appears if '#default_value' is not set AND '#empty_value' is not set AND '#empty_option' is NOT set (?).

That means that if you set a custom '#empty_option', it will always appears, regardless of '#required' and '#default_value'.

If you don't set '#empty_value' nor '#empty_option', the default '#empty_option' will honor '#default_value' for '#required' selects as before.

@see form_process_select(), _form_validate() and form_type_select_value().

Frederic wbase’s picture

Thanks to ptkobe i've fixed it with a form alter.

Here is my code:

function yourmodule_form_alter(&$form, &$form_state, $form_id) {
  switch($form_id) {
  	
    case 'webform_client_form_your_webform_id':
			$form['submitted']['component_name']['#default_value'] = '';
			$form['submitted']['component_name']['#empty_value'] = '';
			$form['submitted']['component_name']['#empty_option'] = '- Select country -'; 

			break;
  }
}


rick.kowal’s picture

Thank you, Frederic wbase & ptkobe. #10 worked for me!

I had a use case where users needed to enter birthdays -- month, day, and year -- in a Birthday fieldset. All fields required and the default options were supposed to read MM, DD, and YYYY (of course I didn't want submissions that were MM/DD/YYYY but instead 01/10/1978)

Using the form_alter in #10:

$form['submitted']['birthday']['birth_month']['#default_value'] = '';
            $form['submitted']['birthday']['birth_month']['#empty_value'] = '';
            $form['submitted']['birthday']['birth_month']['#empty_option'] = 'MM';
            
            $form['submitted']['birthday']['birth_day']['#default_value'] = '';
            $form['submitted']['birthday']['birth_day']['#empty_value'] = '';
            $form['submitted']['birthday']['birth_day']['#empty_option'] = 'DD';
            
            $form['submitted']['birthday']['birth_year']['#default_value'] = '';
            $form['submitted']['birthday']['birth_year']['#empty_value'] = '';
            $form['submitted']['birthday']['birth_year']['#empty_option'] = 'YYYY';

Thanks for the help!

davidsheart02’s picture

Issue summary: View changes

It's been a while but this is exactly what I'm looking to do. Can webform be altered to allow empty keys to be entered for select options and then those options be set as default? While the form_alters are a decent workaround, I'm hoping webform could add this feature into its default functionality.

danchadwick’s picture

Version: 7.x-3.13 » 7.x-4.x-dev

I would be happy to review a patch for the current 7.x-4.x branch. There won't be new features added to the 7.x-3.x branch.

danchadwick’s picture

Title: Allow blank keys in select components to provide custom "Select" or "Choose" labels » Allow select components (list, single-select, no default) to customize name of empty list option
Status: Active » Fixed
Issue tags: -customise the - Select - or - None - option of my select lists
StatusFileSize
new2.31 KB

Much cleaner, and actually easier, to let the user specify this. This is only used when a) the select component is displayed as a list, b) when the list isn't multiple-select, and when c) there is no default specified.

Committed to 7.x-4.x and 8.x.

  • DanChadwick committed aa16c24 on 8.x-4.x
    Issue #1331956 by DanChadwick: Allow select components (list, single-...

  • DanChadwick committed 4a638c1 on
    Issue #1331956 by DanChadwick: Allow select components (list, single-...

Status: Fixed » Closed (fixed)

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

leendertdb’s picture

@ #15,

It seems that this patch has introduced a new bug, which is that if no custom "Empty option" has been provided, the empty option is blank instead of the default Drupal "- Select -" or "- None -".

So it in fact will show up like this in a select list with two options yes/no:

- ""
- "Yes"
- "No"

You can test this yourself by making a new webform on the 7.x-4.8 version with a select field with two options, setting it to required, checking the box "Listbox" and leaving the "Empty option" empty.

After I have done some testing it seems that the added line below is the culprit in components/select.inc on line 352:

   if ($component['extra']['aslist'] && !$component['extra']['multiple'] && $default_value === '') {
     $element['#empty_value'] = '';
     + $element['#empty_option'] = $component['extra']['empty_option'];
   }

If the #empty_option is set (even if it's blank), Drupal won't add it's own default values. And what the above code does is it sets the #empty_option value to $component['extra']['empty_option'], which can be blank since this isn't checked. See Drupal API for form_process_select().

My suggestion would be to change this:

   if ($component['extra']['aslist'] && !$component['extra']['multiple'] && $default_value === '') {
     $element['#empty_value'] = '';
     $element['#empty_option'] = $component['extra']['empty_option'];
   }

to:

   if ($component['extra']['aslist'] && !$component['extra']['multiple'] && $default_value === '') {
     $element['#empty_value'] = '';
   }
   
   if (isset($component['extra']['empty_option']) && strlen($component['extra']['empty_option']) > 0) {
     $element['#empty_option'] = $component['extra']['empty_option'];
   }

That fixes this problem. Any thoughts?

leendertdb’s picture

Status: Closed (fixed) » Active
danchadwick’s picture

Status: Active » Fixed
StatusFileSize
new1.05 KB

Wow, can't believe that slipped through, since obviously I intended this with the description for the form element.

So if you want a blank empty_option, use a space. Really empty (strlen == 0) means default. I'm okay with that.

Committed to 7.x-4.x and 8.x for 4.9.

  • DanChadwick committed 286ff10 on 8.x-4.x
    Issue #1331956 by DanChadwick, leendertdb: Allow blank empty_option for...

  • DanChadwick committed d304685 on
    Issue #1331956 by DanChadwick, leendertdb: Allow blank empty_option for...
leendertdb’s picture

Looks good to me, thanks for the quick response. Glad to be of help. :-)

Status: Fixed » Closed (fixed)

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

joseph.olstad’s picture

at least in our environment it looks like in both the latest dev of 7.x-4.x and latest release of 7.x-4.14 that the empty_option doesn't behave as expected. Rather than displaying '- Select -' by default, all that is seen is '- -'

not a huge problem, as '- -' is probably ok for most users. I couldn't find it in the string translations. But '- -' means probably the same thing in most languages.

Just throught I'd write this observation. spent a few minutes trying to debug it and compare 7.x-4.14 to 7.x-4.x dev , same behavior either way. Haven't yet tested in a fresh install though.

Setareh Farahnak’s picture

Solution number 10 and 11 worked for me, thx guys.