Last updated October 31, 2011. Created by Chris Charlton on October 24, 2008.
Edited by dokumori, quicksketch, aaron_michels, DrewMathers. Log in to edit this page.

Creating radio buttons, checkboxes, and select lists is a little tricky in Webform, since they're all part of the "select" component type. To create these types, follow the instructions below for the type of element you are trying to create.

  • Select list (single selection) - choose the "Select options" type, enter the values and label, and make sure "Listbox" option is checked (on).
  • Select list (multiple selection) - choose the "Select options" type, enter the values and label, then choose the "Multiple" advanced setting, and make sure "Listbox" option is checked (on).
  • Checkbox - choose the "Select options" type, enter the value and label, then choose the "Multiple" advanced setting (even if you just want one checkbox to appear), and make sure "Listbox" option is not on.
  • Checkboxes (multiple) - choose the "Select options" type, enter the values and label, then choose the "Multiple" advanced setting, and make sure "Listbox" option is not on.
  • Radio buttons - choose the "Select options" type, enter the values and label. Make sure both the 'Multiple' advanced setting and "Listbox" option settings are not on.

Options can be grouped for listboxes only ("Listbox" option setting on).

Note if the default option is left blank, an option for "- Please select -" will be added automatically for non-required fields and "- Select an option -" for required field.

Entering options through the interface

For most users, options will be defined through the Webform interface after adding a new "Select options" component. When adding or editing a component, you may specify list options in a textarea. Note that if you install the Option element module, specifying options is much cleaner than the single textarea approach used by default.

Without Options Element module, you'll be given an options box where options are specified as key|value pairs and optionally grouped by using brackets such as <groupname>. For example a country list:

<Europe>
nl|The Netherlands
be|Belgium
fr|France
<Africa>
tu|Tunisia
sa|South Africa
<Asia>
ru|Russia
cn|China
<>
key|some country that is in no group

Defined options through code

If you have a list of data that is regularly customized through some database value or some other dynamic criteria, you may consider having the options generated by a predefined list. Webform includes a few pre-defined lists by default, such as Days of the Week, US States, and Countries of the world (with either Countries API in Drupal 6 or out-of-box with Drupal 7). You can create your own predefined lists by using the webform_webform_select_options_info hook. To do this, create an empty module or edit an existing custom module. In yourmodule.module, add a function yourmodule_webform_select_options_info(). This function must return an array of pre generated select boxes. In order to return the above select box the code would look like this:

<?php
/**
* Implements hook_webform_select_options_info().
*/
function yourmodule_webform_select_options_info() {
 
$items = array();
 
$items['countries'] = array(
   
'title' => t('Countries'),
   
'options callback' => 'webform_options_countries',
  );
  return
$items;
}
/**
* Webform options info callback.
*/
function webform_options_countries() {
 
$countries = array(
   
t('Europe') => array (
     
'nl' =>  t('The Netherlands'),
     
'be' =>  t('Belgium'),
     
'fr' =>  t('France'),
    ),
   
t('Africa') => array (
     
'tu'  =>  t('Tunisia'),
     
'sa'  =>  t('South Africe'),
    ),
   
t('Asia') => array (
     
'ru'  =>  t('Russia'),
     
'cn'  =>  t('China'),
    ),
   
'key' => t('some country that is in no group'),
  );
  return
$countries;
}
?>

Select list extension modules:

  • Options Element: Provides a better UI for creating select list options.
  • County codes API: Adds a countries list to the pre-built options lists.
  • Select (or other): Allows for a textfield for "Other..." options in select lists and radio buttons.
  • View Reference Component: Use a View as the data source for select options instead of hard-coding them.
  • Webform Conditional: Makes a field appear or not depending on the value of a previous select option on the same page. Note: This can be done without Webform Conditional, but the dependent field must be on a separate page.

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

Great! You can create a EULA as the first page using the checkbox option and the "pagebreak" feature (http://drupal.org/node/324779)

Just in case anyone wants to get a list from a taoxonomy vocabulary I did this - although it works it might not be the best solution and I am sure someone from the community will point out a better way but here goes:

<?php
function MY_MODULE_webform_select_options_info() {
 
$items = array();
 
$items['vocabulary_name'] = array(
   
'title' => t('Taxonomy from vocabulary 6'),
   
'options callback' => 'MY_MODULE_options_vocabulary_name',
  );
  return
$items;
}
function
MODULE_options_vocabulary_name() {
 
$vid = 6; // Vocabulary ID
 
$term = taxonomy_get_tree($vid, $parent = 0, $depth = -1, $max_depth = NULL);
 
$vocabulary_name = array();
  foreach (
$term as $key => $value) {
   
$vocabulary_name[$value->tid] = $value->name;
  }
  return
$vocabulary_name;
}
?>

Bear in mind that the keys in the resulting $vocabulary array are the term ID's although you might not need this, you could just as easily use $vocabulary[] if you purely wanted to present a list to someone and not worry about the term ID's being keys in the resulting select list.

It seems that this is a good method. How about integration with AHAH? For example, if the taxonomy contains a list of countries and cities and we have two fields - country and city, and the city depends on the country.

--
Regards,
Alexander Matveev
http://digitalent.ru

I don't get it - after adding this dynamic code to your module, then how do you add the options using the webform UI? There's nothing to choose. Any help?

Morningtime, a digital agency
Drupal services, web development, project management.

This works well.
But how do I set one of these values as default? In your example, if you wanted to make the option with tid 34 default, how would you do that?

By default, select lists read 'none' unless you assign a default.

Is there a way to change the value to something besides 'none' without setting a default? Grouping sounded promising but it doesn't help.

I'm having problem to get token value from core profile to webform. I have some checkboxes and another field with radio buttons in core profile module and I want to populate their values in webform as default values with the help of token module but it does not appear to be working.

Please help

Thanks

Just so you know:

make the first option 0| - Select - or whatever and then put the 0 in the 'Default value' box.

But this isn't going to work for mandatory fields unless you put in some additional validation.

Hi,

I'm looking for a solution to add images as option values. Just like Yes/No, I want Image1/Image2.

Can you someone please help me with this?

Thanks

Alice

Hi,

I have created a text field and I want the name attribute to be "subscriber_firstname", however the module is wrapping this with a submitted[] like so submitted[subscriber_firstname]. Is there a way to remove the submitted[] around my custom name field ?

cheers,

Because of the radios bug in Drupal 7 http://drupal.org/node/811542, you want to avoid using radios as much as possible! Unfortunately you cannot create a single valued set of checkboxes.

0|No
1|Yes

If you set allowed values = 1, then you are FORCED to use a radio. If you select UNLIMITED, then your users can check both of those options. In other words, you are SOL with Drupal 7 and radios. They are fixing this in Drupal 8

I have the basic Webform module: Webform 6.x-3.11. I am trying to add a form with a simple drop menu that should pass a value as a token to the output URL.

The option list for the field with the field key [fleet] is:

bocasmall| 1-4
rmi_large| 5-100

The custom url is set to: %value[fleet] With a redirect set for bocasmall to go elsewhere....

The option list keeps relaying the value 1-4 or 5-100 for the value pairs, instead of the bocasmall/rmi_large value, which is what I would read the docs to say should be passed. ( The result page keeps passing the value 1 4 to the search engine....not what I want)

Also the docs seems to disagree on where the custom URL should read %get[fleet] or %value[fleet] I have tried both but niether work.....

Any suggestions would be welcome.

Ken Pooley
Webmaster
www.SmccME.edu

I am confused as to how to add mandatory fields to a webform contingent on another field.

For example:

Are you bringing children?*MANDATORY*
1| Yes
2| No

If option 1 is selected, I would like the following questions mandatory

Child's Name
Child's Birthdate
Child's Gender

If option 2 is selected, then the above fields should not be made mandatory.

Any advice?

Check out the Conditional Fields module. It will allow you to make things visible/invisible based on choices. It might not work exactly like you want it, but you can always rephrase and restructure your questions and flow to make it just right with the conditional fields module.

For example:

If "Yes", then show questions about children
If "No", then hide questions about children

http://drupal.org/project/conditional_fields

I'm surprised I haven't seen this anywhere; but what about "Check All" and "Uncheck All" options?

Need an All option followed by individual selections. Default to All. If an individual item is selected, the All option is switched off.
[*] All
[ ] A
[ ] B
[ ] C

jQuery could do this. If anything other than All is selected, All is switched off. Based on conventions, jQuery could find elements with a specific class then perform the changes.

Look for div.form-checkboxes
Look inside for input.checkbox value = all
Set jQuery to react on input change within the div.