Validation for multiple Key Values (checkbox) does not seem to work. I keep getting a failure warning.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Liam Morland’s picture

Status: Active » Postponed (maintainer needs more info)

What is the warning? How is the validation rule configured?

Liam Morland’s picture

Status: Postponed (maintainer needs more info) » Closed (works as designed)

Presumed fixed. If you are still having problems, please re-open and provide details.

jebernier’s picture

Sorry, don't get an error, just the the correct answers never pass validation.

I have a multi-choice (checkbox) question and when I enter 2 key values separated by a comma the question always fails when taking the test?

Liam Morland’s picture

Version: 7.x-1.1 » 7.x-1.x-dev
Status: Closed (works as designed) » Active

Do I have this right:

  1. You have a select options form component with Multiple allowed.
  2. You are using the "Specific value(s)" validator and putting more than one key, separated by commas, as the data.
  3. You want it to accept any submission in which the values checked are from the list provided in the data.
  4. The problem is that validation always fails if more than one item is checked in a submission.
jebernier’s picture

Yes, you have it correct. So...how do I correct?

Liam Morland’s picture

If you would like to work on a patch, that would be great. Checkout the source code, see if you can fix the bug, and upload the patch for review. Let me know if you need help doing this. The problem is probably in the file webform_validation.validators.inc in the section where the validation rules are tested.

jebernier’s picture

Sorry, not a real strong php coder. No idea how to do this.

jebernier’s picture

Could use some assistance getting this to work.

John

Liam Morland’s picture

I won't have time to look at this for a while. Have a look at the source code. It is likely not a very complicated problem. The code you need to look at is probably the code in webform_validation.validators.inc that follows this:

case "specific_value":

Be sure to work from the latest development version from version control.

DrewMathers’s picture

Status: Active » Needs review
FileSize
1.02 KB

Here is the section for validating specific values

      case "specific_value":
        $specific_values = explode(',', $rule['data']);
        $specific_values = array_map('trim', $specific_values);
        foreach ($items as $key => $val) {
          if (is_array($val)) {
            $val = _webform_validation_flatten_array($val);
          }
          _webform_validation_test($errors, $key, $rule, !in_array($val, $specific_values));
        }
        return $errors;

The user-entered form data array is being flattened into a string and then compared against the specific_values array. This works fine if the user only enters one thing on the form. If there are multiple entries, then a string like "entry1,entry2,entry3" is compared against each item in the array. The solution is to compare two arrays, like this:

      foreach ($items as $key => $val) {
          if (!is_array($val)) {
            $val = array($val);
          }
          $test = !count(array_intersect(array_unique($val), array_unique($specific_values)));
          _webform_validation_test($errors, $key, $rule, $test);
      }

I have attached a patch.

Liam Morland’s picture

Status: Needs review » Needs work

I don't think your algorithm works. What is $val contains some items that are in $specific_values and some that are not? array_intersect() will still return a non-empty array and validation would succeed.

DrewMathers’s picture

The scenario from #11 would satisfy the test. This validator is non-exclusive OR test.

I used the following scenario in testing this patch:

Create a multi-select (checbox) list using days of the week
Create required key values: tuesday, thursday, saturday

This creates a condition where you MAY select any days of the week, but you MUST include at at least one of Tuesday, Thursday or Saturday. Similar scenarios might be: You MAY select any listed food item, but you MUST include at least one of broccoli, carrots or spinach.

This satisfies the definition of this validator:

Verifies that the specified component contains a defined value
Liam Morland’s picture

I had interpreted this validator to check that all user input much be on the list of allowed values, not just at least one, like a multiple-choice quiz with more than one right answer.

I agree that the wording taken literally means "at least one", but I don't think most people would want or expect that. Perhaps the description should be updated to make it clear.

DrewMathers’s picture

@Liam Morland, For a multi-select list, this interpretation implies that some options are explicitly prohibited. If so, why are they offered on the list at all?

The Select component creates issues for this validator that don't exist when it is applied to other component types. Do you think a special version of the Specific Values validator should be created specifically for Select lists?

@jebernier, without revealing anything confidential, can you give us more detail about the form you are setting up and how you want the multi-select to work? Let's make sure we are solving the right problem.

Liam Morland’s picture

Prohibited options could be offered as a spam check or multiple-choice quiz.

In any case, the most important thing is that the validator does what the users expect. Most won't be either reviewing the source code or extensively testing the various combinations to verify that it behaves as expected.

Certainly, the select component creates unique challenges because it is able to provide more than one answer/value per component. If we need a special validator, we can do that, but let's be clear first on what is needed.

DrewMathers’s picture

I suggest we have it both ways. Have "Specified values" strictly adhere to its description as implemented in the patch from #10 and create a new validator with the description

Verifies that the specified component contains a defined value(s) and excludes any other values

See patch at; #1935686: Specified values (exclusive) validator

Liam Morland’s picture

Status: Needs work » Fixed

I think the intention of this validator is to require that all values of a component are from the permitted list. The following patch achieves that. If there is a demand and use case for the at-least-one validator, please open a separate issue for that.

http://drupalcode.org/project/webform_validation.git/commitdiff/f1ba7d4

Liam Morland’s picture

Version: 7.x-1.x-dev » 6.x-1.x-dev
Status: Fixed » Patch (to be ported)
Liam Morland’s picture

Version: 6.x-1.x-dev » 7.x-1.x-dev
Issue summary: View changes
Status: Patch (to be ported) » Closed (fixed)

Drupal 6 is no longer supported.