public function AmpAnalyticsSettingsForm::validateForm in Accelerated Mobile Pages (AMP) 8.3
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/ AmpAnalyticsSettingsForm.php, line 133
Class
- AmpAnalyticsSettingsForm
- Defines the configuration export form.
Namespace
Drupal\amp\FormCode
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 Tag Manager ID.
if (!empty($form_state
->getValue('gtm_id'))) {
$form_state
->setValue('gtm_id', trim($form_state
->getValue('gtm_id')));
// Replace all type of dashes (n-dash, m-dash, minus) with normal dashes.
$form_state
->setValue('gtm_id', str_replace([
'–',
'—',
'−',
], '-', $form_state
->getValue('gtm_id')));
if (!preg_match('/^GTM-[\\d\\w]+$/', $form_state
->getValue('gtm_id'))) {
$form_state
->setErrorByName('gtm_id', t('A valid Google Tag Manager ID is case sensitive and formatted like GTM-xxxxxxx.'));
}
}
}