You are here

function _name_options_validate in Name Field 7

Checks that individual values in the defined options list do not exceed the limits placed on the component.

2 calls to _name_options_validate()
_name_field_settings_form_validate in includes/name.content.inc
Implements the validation callback for the name_field_settings_form() form.
_webform_name_options_validate in includes/webform.components.inc
Custom validation callback.

File

includes/name.content.inc, line 209
Provides additional Field functionality for the name module.

Code

function _name_options_validate($options, $max, $label, $error_element) {
  $values = array_filter(explode("\n", $options));
  $long_options = array();
  $valid_options = array();
  $default_options = array();
  foreach ($values as $value) {
    $value = trim($value);

    // Blank option - anything goes!
    if (strpos($value, '--') === 0) {
      $default_options[] = $value;
    }
    elseif (preg_match(NAME_FIELD_TAXONOMY_OPTION_REGEX, $value, $matches)) {
      if (!module_exists('taxonomy')) {
        form_set_error($error_element, t("The taxonomy module must be enabled before using the '%tag' tag in %label.", array(
          '%tag' => $matches[0],
          '%label' => $label,
        )));
      }
      elseif ($value !== $matches[0]) {
        form_set_error($error_element, t("The '%tag' tag in %label should be on a line by itself.", array(
          '%tag' => $matches[0],
          '%label' => $label,
        )));
      }
      else {
        if (_name_get_vocabulary_id_by_code_or_number($matches[1])) {
          $valid_options[] = $value;
        }
        else {
          form_set_error($error_element, t("The vocabulary '%tag' in %label could not be found.", array(
            '%tag' => $matches[1],
            '%label' => $label,
          )));
        }
      }
    }
    elseif (drupal_strlen($value) > $max) {
      $long_options[] = $value;
    }
    elseif (!empty($value)) {
      $valid_options[] = $value;
    }
  }
  if (count($long_options)) {
    form_set_error($error_element, t('The following options exceed the maximun allowed %label length: %options', array(
      '%options' => implode(', ', $long_options),
      '%label' => $label,
    )));
  }
  elseif (empty($valid_options)) {
    form_set_error($error_element, t('%label are required.', array(
      '%label' => $label,
    )));
  }
  elseif (count($default_options) > 1) {
    form_set_error($error_element, t('%label can only have one blank value assigned to it.', array(
      '%label' => $label,
    )));
  }
}