0

I need to prevent two different users from choosing the same option from a webform select options component. In the validation only validation required available, no unique validation.

For example: my select contains the following: 1|1, 2|2, 3|3. AnnonUser1 chooses option 2. AnnonUser2 should be presented with 1|1 and 3|3.

How can I validate unique for this select options component. I tried webform validation, but it does not provide unique validation for select options component as well.

2
  • Your question is not very clear. Please describe the select options, how the select is working now, and how you want it to work. Commented May 11, 2015 at 19:15
  • This is the situation. I have the list in the select options component, Item No: 1|1 , 2|2 , .... 200|200. If one select item no 1 and submit, then I want others cannot submit item no 1. Commented May 12, 2015 at 1:11

3 Answers 3

1

You can use a select list with a pre-loaded option list. In that "preload" callback you can alter available options.

Define a custom option list:

function mymodule_webform_select_options_info() {
  $items = array();

  $items['exclusive'] = array(
    'title' => t('Exclusive options'), 
    'options callback' => 'mymodule_webform_options_exclusive', 
  );

  return $items;
}

This callback is called every time the form is displayed, we can alter options here based on previous submissions:

function mymodule_webform_options_exclusive() {
  $default_options = array("One" => 1, "Two" => 2, "Three" => 3);
  $available_values = mymodule_webform_available_values(5); // nid of the webform

  return array_diff($default_options, $available_values);
}

function mymodule_webform_available_values($nid) {
  $submissions = webform_get_submissions(array("nid" => $nid));

  // get all the options values that were already submitted
  $values = array_values(array_map(function($submission) {
    return $submission->data[1][0]; // 1 == index of the field in the form
  }, $submissions));

  return $values;
}

Two things are hard-coded here: nid of the webform (5 here) and the index of the select widget (1 here). It might be possible to avoid hard-coding stuff, good luck!

1
  • I would like to try this, however I don't know how to do that. Can you put in a module package that I can unzip and modify the hard-coded part. Or please give me the instructions how to do that. I tried to create a new folder "webform_exclusive_select" inside "webfform_exclusive_select.module" and copy those three functions. The website becomes messy, the functions appeared. Commented Nov 10, 2015 at 0:37
0

You will almost definitely have to write some custom code to do what you are describing. Start by Googling for "drupal webform dynamic select". There are lots articles that should give you an idea where to start.

1
  • I cannot find a way to prevent similar submission from select options component. The submission should be unique. Commented May 14, 2015 at 22:36
0
var option_value_order = {'volvo', 'saab', 'mercedes'};
var option_display_strings = new Array();
option_display_strings['volvo'] = 'Volvo'; 
//...

// Assume each of the selects' ID value is "select_1", "select_2" instead of "1", "2"
function redraw(selectNum) {
    var selectId = "select_" + selectNum;
    var s = document.getElementById(selectId);
    s.options.length = 0; // empty out
    var next = 0;
    for (var i = 0; i < option_value_order.length; i++) { 
        var skip = 0;
        for (var select_index = 0; select_index < selectNum; select_index++) {
            if (document.getElementById("select_"+select_index).selected == i)
                skip = 1;
            if (skip == 0) {
                var o = option_value_order[i];
                s.options[next] = o;
                // Don't recall how to set value different from option display, 
                // using option_display_strings[o] 
                next++;
            }
        }
    }
}

var total_selects = 2;

function change_later_selects(s) {
    var select_num = find_number(s.id); "this returns number 2 for "select_2" - homework
    for (var i = select_num + 1; i <= total_selects; i++) {
        redraw(i);
    }
}

and then the HTML code

<select id="select_2" onchange="change_later_selects(this);">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
</select>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.