You are here

public function SamlSpConfig::validateForm in SAML Service Provider 8.3

Same name and namespace in other branches
  1. 8.2 src/Form/SamlSpConfig.php \Drupal\saml_sp\Form\SamlSpConfig::validateForm()
  2. 4.x src/Form/SamlSpConfig.php \Drupal\saml_sp\Form\SamlSpConfig::validateForm()
  3. 3.x src/Form/SamlSpConfig.php \Drupal\saml_sp\Form\SamlSpConfig::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/SamlSpConfig.php, line 101

Class

SamlSpConfig
Provides the configuration form.

Namespace

Drupal\saml_sp\Form

Code

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

  // Ensure the cert and key files are provided and exist in the system if
  // signed or encryption options require them.
  $values = $form_state
    ->getValues();
  $org_keys = [
    'name',
    'display_name',
    'url',
  ];
  $org_missing = [];
  foreach ($org_keys as $key) {
    if (empty($values['organization'][$key])) {
      $org_missing[] = $key;
    }
  }
  if (!empty($org_missing) && $org_missing !== $org_keys) {
    foreach ($org_missing as $key) {
      $form_state
        ->setError($form['organization'][$key], $this
        ->t('The %field must be provided for %org.', [
        '%field' => $form['organization'][$key]['#title'],
        '%org' => $form['organization']['#title'],
      ]));
    }
  }
  if ($values['security']['authnRequestsSigned'] || $values['security']['logoutRequestSigned'] || $values['security']['logoutResponseSigned'] || $values['security']['wantNameIdEncrypted'] || $values['security']['signMetaData']) {
    foreach ([
      'key_location',
      'cert_location',
    ] as $key) {
      if (empty($values[$key])) {
        $form_state
          ->setError($form[$key], $this
          ->t('The %field must be provided.', [
          '%field' => $form[$key]['#title'],
        ]));
      }
      elseif (!file_exists($values[$key])) {
        $form_state
          ->setError($form[$key], $this
          ->t('The %input file does not exist.', [
          '%input' => $values[$key],
        ]));
      }
    }
  }
}