You are here

function google_tag_settings_form_validate in GoogleTagManager 7

Same name and namespace in other branches
  1. 7.2 includes/form/settings.inc \google_tag_settings_form_validate()

Form validation handler for google_tag_settings_form().

File

includes/admin.inc, line 144
Contains the administrative page and form callbacks.

Code

function google_tag_settings_form_validate($form, &$form_state) {
  $values =& $form_state['values'];

  // Trim the text values.
  $values['google_tag_container_id'] = trim($values['google_tag_container_id']);
  $values['google_tag_data_layer'] = trim($values['google_tag_data_layer']);
  google_tag_text_clean($values['google_tag_path_list']);
  google_tag_text_clean($values['google_tag_status_list']);
  google_tag_text_clean($values['google_tag_whitelist_classes']);
  google_tag_text_clean($values['google_tag_blacklist_classes']);

  // Replace all types of dashes (n-dash, m-dash, minus) with a normal dash.
  $values['google_tag_container_id'] = str_replace(array(
    '–',
    '—',
    '−',
  ), '-', $values['google_tag_container_id']);
  $values['google_tag_environment_id'] = str_replace(array(
    '–',
    '—',
    '−',
  ), '-', $values['google_tag_environment_id']);
  if (!preg_match('/^GTM-\\w{4,}$/', $values['google_tag_container_id'])) {

    // @todo Is there a more specific regular expression that applies?
    // @todo Is there a way to "test the connection" to determine a valid ID for
    // a container? It may be valid but not the correct one for the website.
    form_set_error('google_tag_container_id', t('A valid container ID is case sensitive and formatted like GTM-xxxxxx.'));
  }
  if ($values['google_tag_include_environment'] && !preg_match('/^env-\\d{1,}$/', $values['google_tag_environment_id'])) {
    form_set_error('google_tag_environment_id', t('A valid environment ID is case sensitive and formatted like env-x.'));
  }
  if ($message = _google_tag_data_layer_verify($values['google_tag_data_layer'])) {
    form_set_error('google_tag_data_layer', $message);
  }
  if ($values['google_tag_include_classes']) {
    if (empty($values['google_tag_whitelist_classes']) && empty($values['google_tag_blacklist_classes'])) {
      form_set_error('google_tag_include_classes', t('Enter listed classes in at least one field, or uncheck the box.'));
      form_set_error('google_tag_whitelist_classes', '');
      form_set_error('google_tag_blacklist_classes', '');
    }
  }
}