You are here

function _webform_edit_validate_select in Webform 5.2

Same name and namespace in other branches
  1. 5 components/select.inc \_webform_edit_validate_select()
  2. 6.3 components/select.inc \_webform_edit_validate_select()
  3. 6.2 components/select.inc \_webform_edit_validate_select()
  4. 7.4 components/select.inc \_webform_edit_validate_select()
  5. 7.3 components/select.inc \_webform_edit_validate_select()

Element validation callback. Ensure keys are not duplicated.

File

components/select.inc, line 89
Webform module multiple select component.

Code

function _webform_edit_validate_select($element) {

  // TODO: Validate e-mail addresses when used as keys?\
  // Check for duplicate key values to prevent unexpected data loss.
  if (!empty($element['#value'])) {
    $lines = explode("\n", $element['#value']);
    $existing_keys = array();
    $duplicate_keys = array();
    $group = '';
    foreach ($lines as $line) {
      $matches = array();
      $line = trim($line);
      if (preg_match('/^\\<([^>]*)\\>$/', $line, $matches)) {
        $group = $matches[1];
        $key = NULL;

        // No need to store group names.
      }
      elseif (preg_match('/^([^|]+)\\|(.*)$/', $line, $matches)) {
        $key = $matches[1];
      }
      else {
        $key = $line;
      }
      if (isset($key)) {
        if (isset($existing_keys[$group][$key])) {
          $duplicate_keys[$key] = $key;
        }
        else {
          $existing_keys[$group][$key] = $key;
        }
      }
    }
    if (!empty($duplicate_keys)) {
      form_error($element, t('Options within the select list must be unique. The following keys have been used multiple times:') . theme('item_list', $duplicate_keys));
    }
  }
  return TRUE;
}