You are here

public function AddUpdateForm::validateForm in Optimizely 8.0

Same name and namespace in other branches
  1. 8 src/AddUpdateForm.php \Drupal\optimizely\AddUpdateForm::validateForm()

Check to make sure the project code is unique except for the default entry which uses the account ID but should support an additional entry to allow for custom settings.

Overrides FormBase::validateForm

File

src/AddUpdateForm.php, line 165
Contains \Drupal\optimizely\AddUpdateForm

Class

AddUpdateForm
Implements the form for the Add Projects page.

Namespace

Drupal\optimizely

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $proj_code = $form_state
    ->getValue('optimizely_project_code');
  $op = $form_state
    ->getValue('op');

  // Watch for "Undefined" value in Project Code, Account ID needed in Settings page
  if ($proj_code == "Undefined") {
    $form_state
      ->setErrorByName('optimizely_project_code', $this
      ->t('The Optimizely Account ID must be set in the' . ' <a href="@url">Account Info</a> page.' . ' The account ID is used as the default Optimizely Project Code.', array(
      '@url' => \Drupal::url('optimizely.settings'),
    )));
  }
  elseif (!ctype_digit($proj_code)) {
    $form_state
      ->setErrorByName('optimizely_project_code', $this
      ->t('The project code !code must only contain digits.', array(
      '!code' => $proj_code,
    )));
  }
  elseif ($op == 'Add') {

    // Confirm project_code is unique or the entered project code is also the account ID.
    // SELECT the project title in prep for related form error message.
    $query = db_query('SELECT project_title FROM {optimizely}
        WHERE project_code = :project_code ORDER BY oid DESC', array(
      ':project_code' => $proj_code,
    ));

    // Fetch an indexed array of the project titles, if any.
    $results = $query
      ->fetchCol(0);
    $query_count = count($results);

    // Flag submission if existing entry is found with the same project code value
    // AND it's not a SINGLE entry to replace the "default" entry.
    if ($query_count > 0 || $proj_code != AccountId::getId() && $query_count >= 2) {

      // Get the title of the project that already had the project code
      $found_entry_title = $results[0];

      // Flag the project code form field
      $form_state
        ->setErrorByName('optimizely_project_code', $this
        ->t('The project code (!project_code) already has an entry' . ' in the "!found_entry_title" project.', array(
        '!project_code' => $proj_code,
        '!found_entry_title' => $found_entry_title,
      )));
    }
  }

  // Skip if disabled entry
  $enabled = $form_state
    ->getValue('optimizely_enabled');
  $paths = $form_state
    ->getValue('optimizely_path');
  $oid = $form_state
    ->getValue('optimizely_oid');
  if ($enabled) {

    // Confirm that the project paths point to valid site URLs
    $target_paths = preg_split('/[\\r\\n]+/', $paths, -1, PREG_SPLIT_NO_EMPTY);
    $valid_path = PathChecker::validatePaths($target_paths);
    if (!is_bool($valid_path)) {
      $form_state
        ->setErrorByName('optimizely_path', t('The project path "@project_path" is not a valid path. The path or alias' . ' could not be resolved as a valid URL that will result in content on the site.', array(
        '@project_path' => $valid_path,
      )));
    }

    // There must be only one Optimizely javascript call on a page.
    // Check paths to ensure there are no duplicates
    // http://support.optimizely.com/customer/portal/questions/893051-multiple-code-snippets-on-same-page-ok-
    list($error_title, $error_path) = PathChecker::uniquePaths($target_paths, $oid);
    if (!is_bool($error_title)) {
      $form_state
        ->setErrorByName('optimizely_path', t('The path "@error_path" will result in a duplicate entry based on' . ' the other project path settings. Optimizely does not allow more' . ' than one project to be run on a page.', array(
        '@error_path' => $error_path,
      )));
    }
  }
}