# How to put select option value into particular category in webform module?

I am using webform module and added a select field.

My option values are

One|Sample 1
Two|Sample 2
Three|Sample 3
Four|Sample 4
Five|Sample 5
Six|Sample 6

But I want these three fields in a particular category named Sample list 1

One|Sample 1
Two|Sample 2
Three|Sample 3

Sample List 2:

Four|Sample 4
Five|Sample 5
Six|Sample 6

Note: I don't want to show it in groups <>.

I want a select list which consists of Sample List 1 and Sample List 2 In a single select field I want to store both of option values into their respective select lists.

• So you want a select list that allows you to choose one value from the first 3 items and one value form the next three items?!? Would it not be easier to just have 2 selects? – Felix Eve Apr 23 '14 at 10:21
• It is a dropdown which I need. You mean to say two select fields? – New Drupal User Apr 23 '14 at 10:23
• Dropdown or select - they are both different names for the same thing. – Felix Eve Apr 23 '14 at 10:24
• Yes right. But I am talking about the options. I need to put the option values in two different categories as I mentioned. – New Drupal User Apr 23 '14 at 10:27
• So you want 2 fields in the database that display as one field to the user? – rooby Apr 23 '14 at 10:39

I think you want to store this a 2 separate fields however present it to the user as 1 field.

One solution would be to use hook_form_alter() to combine the 2 selects into 1, and then put the values back where it should be after the form is submitted.

For this example lets call the first select field A, and the second field B, and the new field that combines both A and B, C.

So - first combine the fields and add an additional submit handler:

function MY_MODULE_form_alter(&$form, &$form_state, $form_id) { if($form_id == 'webform_client_form_<ADD WEBFORM ID HERE>') {

// Make a new field which will have both options. Base in on the existing
// field A.
$form['submitted']['field_c'] =$form['submitted']['field_a'];

// Combine the options of field A and B for the new field.
$form['submitted']['field_c']['#options'] = array_merge($form['submitted']['field_a']['#options'], $form['submitted']['field_b']['#options']); // Hide field A and field B.$form['submitted']['field_a']['#access'] = $form['submitted']['field_b']['#access'] = FALSE; // Add an additional submit handler to put the values back where they // should be. Add this before the existing submit handler so we can make // our changes before anything is saved. array_unshift($form['#submit'], 'split_combined_select_values_out');

// Add a validate function to make sure only 1 option is selected from each category
$form['#validate'][] = 'combined_select_validate'; } }  In the new validation function make sure that only 1 option is selected from each category: function combined_select_validate($form, &$form_state) {$count_a = $count_b = 0; foreach($form_state['values']['submitted']['field_c'] as $val) { if(array_key_exists($val, $form['submitted']['field_a']['#options'])) {$count_a++;
} elseif(array_key_exists($val,$form['submitted']['field_b']['#options'])) {
$count_b++; } } if($count_a > 1 || $count_b > 1) { form_set_error('submitted][field_c', 'Can only select 1 option from each category'); } }  And then in the new submit handler put the values back where they should be: function split_combined_select_values_out($form, &$form_state) { foreach($form_state['values']['submitted']['field_c'] as $val) { // Check if the value selected is from field A or field B if(array_key_exists($val, $form['submitted']['field_a']['#options'])) {$form_state['values']['submitted']['field_a'][$val] =$val;
} elseif(array_key_exists($val,$form['submitted']['field_b']['#options'])) {
$form_state['values']['submitted']['field_b'][$val] = $val; } } // Unset the value from field C. unset($form_state['values']['submitted']['field_c']);

}

• First, I need select fields with option values (2 or more) Secondly, I need another select field with option values belongs to the previous option values(First). Does this do this way? – New Drupal User Apr 23 '14 at 11:08
• I'm not quite sure what you mean I'm afraid. Have a go with the code and see if it helps. – Felix Eve Apr 23 '14 at 11:16
• More Detailed info: In general, dropdown with two values (Sample 1 and Sample 2) which is only shown to admin. Another dropdown with n number of values belongs to sample 1 and n number of values belongs to Sample 2 which is done by admin. The user will be shown only one dropdown of n number of values belongs to both Sample 1 and 2 in the webform. Hope you understood. – New Drupal User Apr 23 '14 at 11:23
• drupal.stackexchange.com/questions/17975/…. Exact match for my question. I guess you can help me out now. The answer is to use [hierarchical_select][1] [1]: drupal.org/project/hierarchical_select module. But I could not able to do what I intended to. – New Drupal User Apr 25 '14 at 12:27