public function AddUpdateForm::validateForm in Optimizely 8
Same name and namespace in other branches
- 8.0 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 158
Class
- AddUpdateForm
- Implements the form for the Add Projects page.
Namespace
Drupal\optimizelyCode
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.', [
'@url' => Url::fromRoute('optimizely.settings')
->toString(),
]));
}
elseif (!ctype_digit($proj_code)) {
$form_state
->setErrorByName('optimizely_project_code', $this
->t("The project code %code must only contain digits.", [
'%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 = \Drupal::database()
->query('SELECT project_title FROM {optimizely}
WHERE project_code = :project_code ORDER BY oid DESC', [
':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.', [
'%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);
// For uniformity and ease of string matching, ensure each path
// starts with a slash / except for the site-wide wildcard * or
// special aliases such as <front>.
self::checkPaths($target_paths);
$valid_path = PathChecker::validatePaths($target_paths);
if (!is_bool($valid_path)) {
$form_state
->setErrorByName('optimizely_path', t('The project path "%project_path" could not be resolved as a valid URL for the site,
or it contains a wildcard * that cannot be handled by this module.', [
'%project_path' => $valid_path,
]));
}
// There must be only one Optimizely javascript call on a page.
// Check paths to ensure there are no duplicates.
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.', [
'%error_path' => $error_path,
]));
}
}
}