You are here

function _webform_edit_validate_select in Webform 7.4

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. 6.2 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.

2 string references to '_webform_edit_validate_select'
_webform_edit_grid in components/grid.inc
Implements _webform_edit_component().
_webform_edit_select in components/select.inc
Implements _webform_edit_component().

File

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

Code

function _webform_edit_validate_select($element, &$form_state) {

  // Check for duplicate key values to prevent unexpected data loss. Require
  // all options to include a safe_key.
  if (!empty($element['#value'])) {
    $lines = explode("\n", trim($element['#value']));
    $existing_keys = array();
    $duplicate_keys = array();
    $missing_keys = array();
    $long_keys = array();
    $group = '';
    foreach ($lines as $line) {
      $matches = array();
      $line = trim($line);
      if (preg_match('/^\\<([^>]*)\\>$/', $line, $matches)) {
        $group = $matches[1];

        // No need to store group names.
        $key = NULL;
      }
      elseif (preg_match('/^([^|]*)\\|(.*)$/', $line, $matches)) {
        $key = $matches[1];
        if (strlen($key) > 128) {
          $long_keys[] = $key;
        }
      }
      else {
        $missing_keys[] = $line;
      }
      if (isset($key)) {
        if (isset($existing_keys[$group][$key])) {
          $duplicate_keys[$key] = $key;
        }
        else {
          $existing_keys[$group][$key] = $key;
        }
      }
    }
    if (!empty($missing_keys)) {
      form_error($element, t('Every option must have a key specified. Specify each option as "safe_key|Some readable option".'));
    }
    if (!empty($long_keys)) {
      form_error($element, t('Option keys must be less than 128 characters. The following keys exceed this limit:') . theme('item_list', $long_keys));
    }
    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', array(
        'items' => $duplicate_keys,
      )));
    }

    // Set the listbox option if needed.
    if (empty($missing_keys) && empty($long_keys) && empty($duplicate_keys)) {
      $options = _webform_select_options_from_text($element['#value']);
      _webform_edit_validate_set_aslist($options, $form_state);
    }
  }
  return TRUE;
}