Webform Conditions for a multi valued select

I’m making a quiz utilising Webform. I would like to use a multi-valued select list for one of the questions, and want to grade the user on any combination of correct answers selected.

Using the Webform Image Select element’s default Cute Kittens as an example, for the values of the select:

  1. kitten_1
  2. kitten_2
  3. kitten_3
  4. kitten_4

I’d like to be able to say a particular combination of selections is the right answer. Say kitten_1 and kitten_3, when they are selected it should show the element which says "Correct answer!".

From what I can see of conditions, they can’t be applied to multiple values. I could create a ‘pattern’ option, but that would only tell me if either kitten_1 or kitten_3 or both is selected, not iff they are both selected.

If this isn’t possible without custom code, can it be added via a plugin etc, I can’t figure that out either.

I wanted to demo this to someone, so I modified the module to add it, diff below, but would very much like a better way.

diff -r /tmp/webform/js/webform.states.js web/modules/contrib/webform/js/webform.states.js 115,119d114 <     else if ('multi_value' in reference) { <      const multiValue = reference['multi_value'].split(','); <      const equal = (multiValue.length === value.length) && (multiValue.sort().every((v, i) => v === value[i])); <      return equal; <     } diff -r /tmp/webform/src/Element/WebformElementStates.php web/modules/contrib/webform/src/Element/WebformElementStates.php 485,486d484 <           [$trigger_selector => ['value' => 'multi_value']], <           'or', 945c943 <       elseif (in_array($trigger, ['multi_value', 'pattern', '!pattern', 'less', 'less_equal', 'greater', 'greater_equal', 'between', '!between'])) { --- >       elseif (in_array($trigger, ['pattern', '!pattern', 'less', 'less_equal', 'greater', 'greater_equal', 'between', '!between'])) { 1105d1102 <       'multi_value' => t('Multi values'), diff -r /tmp/webform/src/WebformEntityConditionsManager.php web/modules/contrib/webform/src/WebformEntityConditionsManager.php 75d74 <       'multi_value' => $this->t('has values'), 233c232 <       if (in_array($trigger_substate, ['multi_value', 'pattern', '!pattern', 'less', 'less_equal', 'greater', 'greater_equal', 'between', '!between'])) { --- >       if (in_array($trigger_substate, ['pattern', '!pattern', 'less', 'less_equal', 'greater', 'greater_equal', 'between', '!between'])) { diff -r /tmp/webform/src/WebformSubmissionConditionsValidator.php web/modules/contrib/webform/src/WebformSubmissionConditionsValidator.php 764c764 <       if (in_array($trigger_substate, ['multi_value', 'pattern', '!pattern', 'less', 'less_equal', 'greater', 'greater_equal', 'between', '!between'])) { --- >       if (in_array($trigger_substate, ['pattern', '!pattern', 'less', 'less_equal', 'greater', 'greater_equal', 'between', '!between'])) { 775,778c775 <     if ($element_plugin->hasMultipleValues($element) && $trigger === 'multi_value') { <       $result = $this->checkConditionTrigger($trigger, $trigger_value, (array) $element_value); <     } <     elseif ($element_plugin->hasMultipleValues($element) && $trigger !== 'empty') { --- >     if ($element_plugin->hasMultipleValues($element) && $trigger !== 'empty') { 832,837d828 <  <       case 'multi_value': <         sort($element_value); <         $multi_values = explode(',', $trigger_value); <         sort($multi_values);    <         return $multi_values === $element_value; 
This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Webform Conditions for a multi valued select

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.