You are here

public function VerifyPhoneNumberForm::validateForm in SMS Framework 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/VerifyPhoneNumberForm.php, line 84

Class

VerifyPhoneNumberForm
Form to accept a verification code.

Namespace

Drupal\sms\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $flood_window = $this
    ->config('sms.settings')
    ->get('flood.verify_window');
  $flood_limit = $this
    ->config('sms.settings')
    ->get('flood.verify_limit');
  if (!$this->flood
    ->isAllowed('sms.verify_phone_number', $flood_limit, $flood_window)) {
    $form_state
      ->setError($form, $this
      ->t('There has been too many failed verification attempts. Try again later.'));
    return;
  }
  $current_time = $this
    ->getRequest()->server
    ->get('REQUEST_TIME');
  $code = $form_state
    ->getValue('code');
  $phone_verification = $this->phoneNumberVerification
    ->getPhoneVerificationByCode($code);
  if ($phone_verification && !$phone_verification
    ->getStatus()) {
    $entity = $phone_verification
      ->getEntity();
    $phone_number_settings = $this->phoneNumberVerification
      ->getPhoneNumberSettingsForEntity($entity);
    $lifetime = $phone_number_settings
      ->getVerificationCodeLifetime() ?: 0;
    if ($current_time > $phone_verification
      ->getCreatedTime() + $lifetime) {
      $form_state
        ->setError($form['code'], $this
        ->t('Verification code is expired.'));
    }
  }
  else {
    $form_state
      ->setError($form['code'], $this
      ->t('Invalid verification code.'));
  }
  $this->flood
    ->register('sms.verify_phone_number', $flood_window);
}