You are here

public function CasSettings::validateForm in CAS 8

Same name and namespace in other branches
  1. 2.x src/Form/CasSettings.php \Drupal\cas\Form\CasSettings::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/CasSettings.php, line 521

Class

CasSettings
Class CasSettings.

Namespace

Drupal\cas\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $condition_values = (new FormState())
    ->setValues($form_state
    ->getValue([
    'gateway',
    'paths',
  ]));
  $this->gatewayPaths
    ->validateConfigurationForm($form, $condition_values);
  $condition_values = (new FormState())
    ->setValues($form_state
    ->getValue([
    'forced_login',
    'paths',
  ]));
  $this->forcedLoginPaths
    ->validateConfigurationForm($form, $condition_values);
  $ssl_verification_method = $form_state
    ->getValue([
    'server',
    'verify',
  ]);
  $cert_path = $form_state
    ->getValue([
    'server',
    'cert',
  ]);
  if ($ssl_verification_method == CasHelper::CA_CUSTOM && !file_exists($cert_path)) {
    $form_state
      ->setErrorByName('server][cert', $this
      ->t('The path you provided to the custom PEM certificate for your CAS server does not exist or is not readable. Verify this path and try again.'));
  }
  if ($form_state
    ->getValue([
    'user_accounts',
    'auto_register',
  ])) {
    $email_assignment_strategy = $form_state
      ->getValue([
      'user_accounts',
      'email_assignment_strategy',
    ]);
    if ($email_assignment_strategy == CasUserManager::EMAIL_ASSIGNMENT_STANDARD && empty($form_state
      ->getValue([
      'user_accounts',
      'email_hostname',
    ]))) {
      $form_state
        ->setErrorByName('user_accounts][email_hostname', $this
        ->t('You must provide a hostname for the auto assigned email address.'));
    }
    elseif ($email_assignment_strategy == CasUserManager::EMAIL_ASSIGNMENT_ATTRIBUTE && empty($form_state
      ->getValue([
      'user_accounts',
      'email_attribute',
    ]))) {
      $form_state
        ->setErrorByName('user_accounts][email_attribute', $this
        ->t('You must provide an attribute name for the auto assigned email address.'));
    }
    if ($form_state
      ->getValue([
      'server',
      'version',
    ]) == '1.0' && $email_assignment_strategy == CasUserManager::EMAIL_ASSIGNMENT_ATTRIBUTE) {
      $form_state
        ->setErrorByName('user_accounts][email_assignment_strategy', $this
        ->t("The CAS protocol version you've specified does not support attributes, so you cannot assign user emails from a CAS attribute value."));
    }
  }
  $error_page_val = $form_state
    ->getValue([
    'error_handling',
    'login_failure_page',
  ]);
  if ($error_page_val) {
    $error_page_val = trim($error_page_val);
    if (strpos($error_page_val, '/') !== 0) {
      $form_state
        ->setErrorByName('error_handling][login_failure_page', $this
        ->t('Path must begin with a forward slash.'));
    }
  }
  return parent::validateForm($form, $form_state);
}