You are here

public function AmpSettingsForm::validateForm in Accelerated Mobile Pages (AMP) 8

Same name and namespace in other branches
  1. 8.2 src/Form/AmpSettingsForm.php \Drupal\amp\Form\AmpSettingsForm::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/AmpSettingsForm.php, line 275

Class

AmpSettingsForm
Defines the configuration export form.

Namespace

Drupal\amp\Form

Code

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

  // Validate the Google Analytics ID.
  if (!empty($form_state
    ->getValue('google_analytics_id'))) {
    $form_state
      ->setValue('google_analytics_id', trim($form_state
      ->getValue('google_analytics_id')));

    // Replace all type of dashes (n-dash, m-dash, minus) with normal dashes.
    $form_state
      ->setValue('google_analytics_id', str_replace([
      '–',
      '—',
      '−',
    ], '-', $form_state
      ->getValue('google_analytics_id')));
    if (!preg_match('/^UA-\\d+-\\d+$/', $form_state
      ->getValue('google_analytics_id'))) {
      $form_state
        ->setErrorByName('google_analytics_id', t('A valid Google Analytics Web Property ID is case sensitive and formatted like UA-xxxxxxx-yy.'));
    }
  }

  // Validate the Google Adsense ID.
  if (!empty($form_state
    ->getValue('google_adsense_id'))) {
    $form_state
      ->setValue('google_adsense_id', trim($form_state
      ->getValue('google_adsense_id')));
    if (!preg_match('/^pub-[0-9]+$/', $form_state
      ->getValue('google_adsense_id'))) {
      $form_state
        ->setErrorByName('google_adsense_id', t('A valid Google AdSense Publisher ID is case sensitive and formatted like pub-9999999999999'));
    }
  }
}