You are here

function webform_edit_matrix_select in Webform Matrix Component 7

Validate webform_edit_form matrix components.

1 string reference to 'webform_edit_matrix_select'
webform_matrix_get_column_form in components/matrix.inc
Webform_edit_form extra form elements.

File

components/matrix.inc, line 488
Webform module matrix component.

Code

function webform_edit_matrix_select($element, &$form_state) {
  $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,
    )));
  }
}