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:

/**
 * 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.
  • Country 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.

Comments

Aaron Stanush’s picture

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

soulston’s picture

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.

Alexander Matveev’s picture

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.

Anonymous’s picture

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?

rwhittaker’s picture

I'm asking the same question. OK. I have created a blank module, and added the necessary code to it.. Now what?

DeNelo’s picture

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?

DarylOrtiz’s picture

Just to let you know, the above code is not working for me. Did try with mutiple queries and even after system restart.

Thanks
VirtueReview.com

escoles’s picture

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.

alice-kahn’s picture

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

bloke_zero’s picture

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.

alice-kahn’s picture

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

tansta’s picture

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,

earwax’s picture

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

pooley’s picture

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.

Joseph0717’s picture

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?

earwax’s picture

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

albert9000’s picture

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

peterx’s picture

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.

Padmakani’s picture

1.Select the select options form field
2.Type in options in the format key|value
3.Check multiple
4. Check Randomize options

Anonymous’s picture

how can I make a list of checkboxes where every item on the list is mandatory. Basically I have a registration page where the user needs to select a list checkboxes (all mandatory) before proceeding to the next field.

Vaibhav_Drupal’s picture

I have a requirement for Webform (D7) where i have a selection option in which i have two option groups and multiple options under those groups. I want to send a Email , subject of which has to include the Option Group and Option.
Eg -
"< Europe >"
be|Belgium
fr|France
"< Africa >"
tu|Tunisia
sa|South Africa

I want to send a Email subject as "Europe: Belgium"

Is there any token / way to directly access the Option Group value for the selected option?

DarylOrtiz’s picture

nice, but am too looking for solution to this.

DarylOrtiz

bankers adda’s picture

//In initialization code:
ImageIcon leftButtonIcon = createImageIcon("images/right.gif");
ImageIcon middleButtonIcon = createImageIcon("images/middle.gif");
ImageIcon rightButtonIcon = createImageIcon("images/left.gif");

b1 = new JButton("Disable middle button", leftButtonIcon);
b1.setVerticalTextPosition(AbstractButton.CENTER);
b1.setHorizontalTextPosition(AbstractButton.LEADING); //aka LEFT, for left-to-right locales
b1.setMnemonic(KeyEvent.VK_D);
b1.setActionCommand("disable");

b2 = new JButton("Middle button", middleButtonIcon);
b2.setVerticalTextPosition(AbstractButton.BOTTOM);
b2.setHorizontalTextPosition(AbstractButton.CENTER);
b2.setMnemonic(KeyEvent.VK_M);

b3 = new JButton("Enable middle button", rightButtonIcon);
//Use the default text position of CENTER, TRAILING (RIGHT).
b3.setMnemonic(KeyEvent.VK_E);
b3.setActionCommand("enable");
b3.setEnabled(false);

//Listen for actions on buttons 1 and 3.
b1.addActionListener(this);
b3.addActionListener(this);

b1.setToolTipText("Click this button to disable "
+ "the middle button.");
b2.setToolTipText("This middle button does nothing "
+ "when you click it.");
b3.setToolTipText("Click this button to enable the "
+ "middle button.");
...
}

public void actionPerformed(ActionEvent e) {
if ("disable".equals(e.getActionCommand())) {
b2.setEnabled(false);
b1.setEnabled(false);
b3.setEnabled(true);
} else {
b2.setEnabled(true);
b1.setEnabled(true);
b3.setEnabled(false);
}
}

protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = ButtonDemo.class.getResource(path);
...//error handling omitted for clarity...
return new ImageIcon(imgURL);

thanks

http://www.bankersadda.net

DarylOrtiz’s picture

when i tried to paste this code, it didn't worked. Here we have put many things including your code, and the specific part of it;

b3 = new JButton("Enable middle button", rightButtonIcon);
//Use the default text position of CENTER, TRAILING (RIGHT).
b3.setMnemonic(KeyEvent.VK_E);
b3.setActionCommand("enable");
b3.setEnabled(false);

Shows big error, I guess the command should be changed to make it more relevant.

Thanks

Bestreviewsage.com