You are here

function _webform_edit_validate_select in Webform 6.2

Same name and namespace in other branches
  1. 5.2 components/select.inc \_webform_edit_validate_select()
  2. 5 components/select.inc \_webform_edit_validate_select()
  3. 6.3 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.

1 string reference to '_webform_edit_validate_select'
_webform_edit_select in components/select.inc
Create a set of form items to be displayed on the form for editing this component. Use care naming the form items, as this correlates directly to the database schema. The component "Name" and "Description" fields are added to every…

File

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

Code

function _webform_edit_validate_select($element, $form_state) {

  // 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;
}