You are here

public function ExampleForm::validateForm in Mime Mail 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

modules/mimemail_example/src/Form/ExampleForm.php, line 176

Class

ExampleForm
The example email contact form.

Namespace

Drupal\mimemail_example\Form

Code

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

  // Extract the address part of the entered email before trying to validate.
  // The email.validator service does not work on RFC2822 formatted addresses
  // so we need to extract the RFC822 part out first. This is not as good as
  // actually validating the full RFC2822 address, but it is better than
  // either just validating RFC822 or not validating at all.
  $pattern = '/<(.*?)>/';
  $address = $form_state
    ->getValue('to');
  preg_match_all($pattern, $address, $matches);
  $address = isset($matches[1][0]) ? $matches[1][0] : $address;
  if (!$this->emailValidator
    ->isValid($address)) {
    $form_state
      ->setErrorByName('to', $this
      ->t('That email address is not valid.'));
  }
  $file = file_save_upload('attachment', [], 'temporary://', 0);
  if ($file) {
    $form_state
      ->setValue([
      'params',
      'attachments',
    ], [
      [
        'filepath' => $file
          ->getFileUri(),
      ],
    ]);
  }
}