You are here

function apachesolr_environment_edit_validate in Apache Solr Search 8

Same name and namespace in other branches
  1. 6.3 apachesolr.admin.inc \apachesolr_environment_edit_validate()
  2. 7 apachesolr.admin.inc \apachesolr_environment_edit_validate()

Validate handler for the environment edit page

_state

Parameters

array $form:

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 268
Administrative pages for the Apache Solr framework.

Code

function apachesolr_environment_edit_validate(array $form, array &$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");
    }
  }
}