You are here

public function SMTPConfigForm::validateForm in SMTP Authentication Support 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/SMTPConfigForm.php, line 352

Class

SMTPConfigForm
Implements the SMTP admin settings form.

Namespace

Drupal\smtp\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();
  if ($values['smtp_on'] !== 'off' && $values['smtp_host'] == '') {
    $form_state
      ->setErrorByName('smtp_host', $this
      ->t('You must enter an SMTP server address.'));
  }
  if ($values['smtp_on'] !== 'off' && $values['smtp_port'] == '') {
    $form_state
      ->setErrorByName('smtp_port', $this
      ->t('You must enter an SMTP port number.'));
  }
  if ($values['smtp_timeout'] == '' || $values['smtp_timeout'] < 1) {
    $form_state
      ->setErrorByName('smtp_timeout', $this
      ->t('You must enter a Timeout value greater than 0.'));
  }
  if ($values['smtp_from'] && !$this->emailValidator
    ->isValid($values['smtp_from'])) {
    $form_state
      ->setErrorByName('smtp_from', $this
      ->t('The provided from e-mail address is not valid.'));
  }
  if ($values['smtp_test_address'] && !$this->emailValidator
    ->isValid($values['smtp_test_address'])) {
    $form_state
      ->setErrorByName('smtp_test_address', $this
      ->t('The provided test e-mail address is not valid.'));
  }
  if ($values['smtp_reroute_address'] && !$this->emailValidator
    ->isValid($values['smtp_reroute_address'])) {
    $form_state
      ->setErrorByName('smtp_reroute_address', $this
      ->t('The provided reroute e-mail address is not valid.'));
  }

  // If username is set empty, we must set both
  // username/password empty as well.
  if (empty($values['smtp_username'])) {
    $values['smtp_password'] = '';
  }
  elseif (empty($values['smtp_password'])) {
    $form_state
      ->unsetValue('smtp_password');
  }
}