I'm using a validation rule to ensure that the user enter distinct values in a specific components list.

I copy my code, maybe it will be useful to someone or could be incorporated into the module.

/**
 * Implements hook_webform_validation_validators().
 */
function YOURMODULE_webform_validation_validators() {
  return array(
    'distinctvalues' => array(
      'name' => t('Distinct values'),
      'component_types' => array(
        'number',
        'textfield',
        'email',
        'select',
        'hidden',
      ),
      'min_components' => 2,
      'description' => t('Verifies that all specified components contain distinct values.'),
    ),
  );
}

/**
 * Implements hook_webform_validation_validate().
 */
function YOURMODULE_webform_validation_validate($validator_name, $items, $components, $rule) {
  if ($items) {
    switch ($validator_name) {
      case 'distinctvalues':
		$values_count = array_count_values($items);
        foreach ($values_count as $key => $val) {
		  if (!empty($key) && $val > 1) {
			$errors[] = t('You have selected %count times the value %value. You cannot repeat values.', array('%count' => $val, '%value' => $key));
		  }
        }
        return $errors;
        break;
    }
  }
}

Comments

oriol_e9g’s picture

Issue summary: View changes
Liam Morland’s picture

Is this not the same as the unique validator?

oriol_e9g’s picture

The "unique validator" validates that the value doesn't match with the values of other users, for example an ID or email. The distinct validator ensures that the user don't repeat values in his form, don't compare with other users values. For exemple:

Which Is your favorite fruit? Order from 1 to 3
- Apple: 3
- Strawberry: 1
- Banana: 2

distinctvalues: OK

- Apple: 2
- Strawberry: 1
- Banana: 2

distinctvalues: Error. 'You have selected 2 times the value '2'. You cannot repeat values.

This validator is useful for forms that uses preferential sistem voting like Borda Count (http://en.wikipedia.org/wiki/Borda_count), IRV (http://en.wikipedia.org/wiki/Instant-runoff_voting) or similars.

Liam Morland’s picture

The unique validation in Webform checks for uniqueness between users. The unique validator in Webform Validation checks multiple components in the same submission. Here is the description:

Verifies that none of the specified components contain the same value as another selected component in this submission. (To check that values are unique between submissions, use the unique validation option on the "Edit component" page for that component.)

Liam Morland’s picture

Category: Feature request » Support request
Status: Active » Fixed

If this does not do what you want, please re-open and provide details.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.