You are here

public function ProtectedPagesLoginForm::validateForm in Protected Pages 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/ProtectedPagesLoginForm.php, line 147

Class

ProtectedPagesLoginForm
Provides login screen to access protected page.

Namespace

Drupal\protected_pages\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $config = $this
    ->config('protected_pages.settings');
  $global_password_setting = $config
    ->get('password.per_page_or_global');
  if ($global_password_setting == 'per_page_password') {
    $fields = [
      'password',
    ];
    $conditions = [];
    $conditions['general'][] = [
      'field' => 'pid',
      'value' => $form_state
        ->getValue('protected_page_pid'),
      'operator' => '=',
    ];
    $password = $this->protectedPagesStorage
      ->loadProtectedPage($fields, $conditions, TRUE);
    if (!$this->password
      ->check($form_state
      ->getValue('password'), $password)) {
      $form_state
        ->setErrorByName('password', $config
        ->get('others.protected_pages_incorrect_password_msg'));
    }
  }
  elseif ($global_password_setting == 'per_page_or_global') {
    $fields = [
      'password',
    ];
    $conditions = [];
    $conditions['general'][] = [
      'field' => 'pid',
      'value' => $form_state
        ->getValue('protected_page_pid'),
      'operator' => '=',
    ];
    $password = $this->protectedPagesStorage
      ->loadProtectedPage($fields, $conditions, TRUE);
    $global_password = $config
      ->get('password.protected_pages_global_password');
    if (!$this->password
      ->check($form_state
      ->getValue('password'), $password) && !$this->password
      ->check($form_state
      ->getValue('password'), $global_password)) {
      $form_state
        ->setErrorByName('password', $config
        ->get('others.protected_pages_incorrect_password_msg'));
    }
  }
  else {
    $global_password = $config
      ->get('password.protected_pages_global_password');
    if (!$this->password
      ->check($form_state
      ->getValue('password'), $global_password)) {
      $form_state
        ->setErrorByName('password', $config
        ->get('others.protected_pages_incorrect_password_msg'));
    }
  }
}