You are here

function apachesolr_environment_edit_validate in Apache Solr Search 6.3

Same name and namespace in other branches
  1. 8 apachesolr.admin.inc \apachesolr_environment_edit_validate()
  2. 7 apachesolr.admin.inc \apachesolr_environment_edit_validate()
1 string reference to 'apachesolr_environment_edit_validate'
apachesolr_environment_edit_form in ./apachesolr.admin.inc
Form builder for adding/editing a Solr environment used as a menu callback.

File

./apachesolr.admin.inc, line 232
Administrative pages for the Apache Solr framework.

Code

function apachesolr_environment_edit_validate($form, &$form_state) {
  $parts = parse_url($form_state['values']['url']);
  foreach (array(
    'scheme',
    'host',
    'path',
  ) as $key) {
    if (empty($parts[$key])) {
      form_set_error('url', t('The Solr server URL needs to include a !part', array(
        '!part' => $key,
      )));
    }
  }
  if (isset($parts['port'])) {

    // parse_url() should always give an integer for port. Since drupal_http_request()
    // also uses parse_url(), we don't need to validate anything except the range.
    $pattern = empty($parts['user']) ? '@://[^:]+:([^/]+)@' : '#://[^@]+@[^:]+:([^/]+)#';
    preg_match($pattern, $form_state['values']['url'], $m);
    if (empty($m[1]) || !ctype_digit($m[1]) || $m[1] < 1 || $m[1] > 65535) {
      form_set_error('port', t('The port has to be an integer between 1 and 65535.'));
    }
    else {

      // Normalize the url by removing extra slashes and whitespace.
      $form_state['values']['url'] = trim($form_state['values']['url'], "/ \t\r\n\0\v");
    }
  }
  $env_id = strtolower(preg_replace(array(
    '/[^a-zA-Z0-9]+/',
    '/-+/',
    '/^-+/',
    '/-+$/',
  ), array(
    '_',
    '_',
    '',
    '',
  ), $form_state['values']['env_id']));
  if ($form_state['values']['env_id'] != $env_id && !empty($env_id)) {
    form_set_error('env_id', t('A unique machine-readable identifier for the search page configuration. It must only contain lowercase letters, numbers, and underscores.'));
  }
  elseif (empty($form['env_id']['#disabled']) && apachesolr_environment_load($env_id)) {
    form_set_error('env_id', t('That indentifier is already in use.'));
  }
}