You are here

public function FormsStepsEditForm::validateForm in Forms Steps 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

src/Form/FormsStepsEditForm.php, line 460

Class

FormsStepsEditForm
Provides a form to edit a Forms Steps.

Namespace

Drupal\forms_steps\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);
  $values = $form_state
    ->getValues();
  switch ($values['redirection_policy']) {

    // Check that the specified internal path exists and that the current user
    // has access to it.
    case 'internal':
      if (!$this->pathValidator
        ->isValid($values['redirection_target'])) {
        $form_state
          ->setErrorByName('redirection_target', $this
          ->t('Invalid internal path for redirection!'));
      }
      break;

    // Check that the specified route exists.
    case 'route':
      if (count($this->routeProvider
        ->getRoutesByNames([
        $values['redirection_target'],
      ])) === 0) {
        $form_state
          ->setErrorByName('redirection_target', $this
          ->t('Invalid route specified for redirection!'));
      }
      break;

    // Check that the specified external URL exists.
    case 'external':
      if (!UrlHelper::isExternal($values['redirection_target']) || !UrlHelper::isValid($values['redirection_target'])) {
        $form_state
          ->setErrorByName('redirection_target', $this
          ->t('Invalid external URL specified for redirection!'));
      }
      break;
  }
}