You are here

public function DomainForm::validateForm in Domain Access 8

Form validation handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormBase::validateForm

File

domain/src/DomainForm.php, line 169

Class

DomainForm
Base form for domain edit forms.

Namespace

Drupal\domain

Code

public function validateForm(array &$form, FormStateInterface $form_state) {

  /** @var \Drupal\domain\DomainInterface $entity */
  $entity = $this->entity;
  $hostname = $entity
    ->getHostname();
  $errors = $this->validator
    ->validate($hostname);
  if (!empty($errors)) {

    // Render errors to display as message.
    $message = [
      '#theme' => 'item_list',
      '#items' => $errors,
    ];
    $message = $this->renderer
      ->renderPlain($message);
    $form_state
      ->setErrorByName('hostname', $message);
  }

  // Validate if the same hostname exists.
  // Do not use domain loader because it may change hostname.
  $existing = $this->domainStorage
    ->loadByProperties([
    'hostname' => $hostname,
  ]);
  $existing = reset($existing);

  // If we have already registered a hostname, make sure we don't create a
  // duplicate.
  // We cannot check id() here, as the machine name is editable.
  if ($existing && $existing
    ->getDomainId() != $entity
    ->getDomainId()) {
    $form_state
      ->setErrorByName('hostname', $this
      ->t('The hostname is already registered.'));
  }

  // Is validate_url set?
  if ($entity->validate_url) {

    // Check the domain response. First, clear the path value.
    $entity
      ->setPath();

    // Check the response.
    $response = $this->validator
      ->checkResponse($entity);

    // If validate_url is set, then we must receive a 200 response.
    if ($response !== 200) {
      if (empty($response)) {
        $response = 500;
      }
      $form_state
        ->setErrorByName('hostname', $this
        ->t('The server request to @url returned a @response response. To proceed, disable the <em>Test server response</em> in the form.', [
        '@url' => $entity
          ->getPath(),
        '@response' => $response,
      ]));
    }
  }
}