You are here

public function UrlRedirectForm::validateForm in Url Redirect 8.2

Same name and namespace in other branches
  1. 8 src/Form/UrlRedirectForm.php \Drupal\url_redirect\Form\UrlRedirectForm::validateForm()

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

src/Form/UrlRedirectForm.php, line 176

Class

UrlRedirectForm
Form handler for the Example add and edit forms.

Namespace

Drupal\url_redirect\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();
  $path = $values['path'];
  $url_redirect = $this->entity;
  $storage = $this->entityTypeManagerInterface
    ->getStorage('url_redirect');
  $path_check = $storage
    ->getQuery()
    ->condition('path', $path)
    ->execute();
  if ($path_check && $url_redirect
    ->isNew()) {
    $form_state
      ->setErrorByName('path', $this
      ->t("The path '@link_path' already used for redirect.", array(
      '@link_path' => $path,
    )));
  }
  $redirect_path = $values['redirect_path'];
  if (!\Drupal::service('path.validator')
    ->isValid($redirect_path)) {
    $form_state
      ->setErrorByName('redirect_path', $this
      ->t("The redirect path '@link_path' is either invalid or you do not have access to it.", array(
      '@link_path' => $redirect_path,
    )));
  }
  $checked_for = $values['checked_for'];

  // Get Checked for Role.
  if ($checked_for == 'Role') {
    $roles_values = $values['roles'];
    if (!$roles_values) {
      $form_state
        ->setErrorByName('roles', $this
        ->t("Select at least one role for which you want to apply this redirect."));
    }
  }
  if ($checked_for == 'User') {
    $users_values = $values['user'];
    if (!$users_values) {
      $form_state
        ->setErrorByName('user', $this
        ->t("Enter at least one user for which you want to apply this redirect."));
    }
  }
}