You are here

public function RoleLoginPageSettings::validateForm in Multiple role login pages 8

@global type $base_url

Parameters

array $form:

FormStateInterface $form_state:

Overrides FormBase::validateForm

File

src/Form/RoleLoginPageSettings.php, line 114
Contains \Drupal\role_login_page\Form\RoleLoginPageSettings.

Class

RoleLoginPageSettings
Add login page form.

Namespace

Drupal\role_login_page\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  global $base_url;
  $url = trim($form_state
    ->getValue([
    'loginmenu_url',
  ]));
  $complete_url = $base_url . '/' . $url;
  $complete_url = filter_var($complete_url, FILTER_SANITIZE_URL);
  $replacements = [
    '!',
    '*',
    "(",
    ")",
    ";",
    "@",
    "+",
    "\$",
    ",",
    "[",
    "]",
  ];
  $complete_url = str_replace($replacements, '', $complete_url);
  if (!filter_var($complete_url, FILTER_VALIDATE_URL)) {
    $form_state
      ->setErrorByName('loginmenu_url', $this
      ->t("@comurl is not a valid URL", [
      '@comurl' => $complete_url,
    ]));
  }
  $menu_exists = \Drupal::service('path.validator')
    ->getUrlIfValid($url);
  if ($menu_exists) {
    $form_state
      ->setErrorByName('loginmenu_url', $this
      ->t('The menu URL already exists'));
  }
  $redirect_path = trim($form_state
    ->getValue([
    'redirect_path',
  ]));
  if ($redirect_path) {
    $redirect_path_exists = \Drupal::service('path.validator')
      ->getUrlIfValid($redirect_path);
    if ($redirect_path_exists) {
      if (!$redirect_path_exists
        ->getRouteName()) {
        $form_state
          ->setErrorByName('redirect_path', $this
          ->t('Please enter a valid redirect path.'));
      }
    }
    else {
      $form_state
        ->setErrorByName('redirect_path', $this
        ->t('Please enter a valid redirect path.'));
    }
  }
}