public static function WebformValidateConstraint::validateFrontCompareComponent in Webform Validation 8
Same name and namespace in other branches
- 2.0.x src/Validate/WebformValidateConstraint.php \Drupal\webform_validation\Validate\WebformValidateConstraint::validateFrontCompareComponent()
Validates compare fields.
Parameters
array $element: The form elements.
\Drupal\Core\Form\FormStateInterface $formState: The form state.
array $form: The form array.
1 call to WebformValidateConstraint::validateFrontCompareComponent()
- WebformValidateConstraint::validateElements in src/
Validate/ WebformValidateConstraint.php - Validates element.
File
- src/
Validate/ WebformValidateConstraint.php, line 196
Class
- WebformValidateConstraint
- Form API callback. Validate element value.
Namespace
Drupal\webform_validation\ValidateCode
public static function validateFrontCompareComponent(array &$element, FormStateInterface $formState, array &$form) : void {
$webformKey = $element['#webform_key'];
$compareWithField = $element['#compare_components'];
$compareOperator = $element['#compare_components_operator'];
$compareErrorMsg = $element['#compare_components_custom_error'];
$thisValue = is_array($formState
->getValue($webformKey)) ? $formState
->getValue($webformKey) : [
$formState
->getValue($webformKey),
];
$submittedValues = $formState
->cleanValues()
->getValues();
$compareWithValue = is_array($submittedValues[$compareWithField]) ? $submittedValues[$compareWithField] : [
$submittedValues[$compareWithField],
];
$storage = $formState
->getStorage();
$visitedElements = !empty($storage['visited']) ? $storage['visited'] : [];
$found = FALSE;
$fieldElement = [];
self::getFormElementAccess($form['elements'], $compareWithField, $found, $fieldElement);
if (!empty($fieldElement['access']) && !in_array($compareWithField, $visitedElements)) {
$visitedElements[] = $compareWithField;
}
if (!empty($element['#access']) && !in_array($webformKey, $visitedElements)) {
$visitedElements[] = $webformKey;
}
$error = FALSE;
if ((!empty($compareWithValue[0]) || !empty($thisValue[0])) && in_array($compareWithField, $visitedElements) && in_array($webformKey, $visitedElements)) {
if (empty($compareWithValue[0]) || empty($thisValue[0])) {
$error = TRUE;
}
elseif (!empty($compareWithValue[0]) && !empty($thisValue[0])) {
switch ($compareOperator) {
case '>':
if (!(min($compareWithValue) > max($thisValue))) {
$error = TRUE;
}
break;
case '>=':
if (!(min($compareWithValue) >= max($thisValue))) {
$error = TRUE;
}
break;
case '<':
if (!(max($compareWithValue) < min($thisValue))) {
$error = TRUE;
}
break;
case '<=':
if (!(max($compareWithValue) <= min($thisValue))) {
$error = TRUE;
}
break;
}
}
}
$storage['visited'] = $visitedElements;
$formState
->setStorage($storage);
if ($error) {
if (empty($fieldElement['access']) || !empty($element['#access']) && empty($thisValue[0])) {
$fieldElement = $element;
}
if (isset($fieldElement['#title'])) {
$formState
->setError($fieldElement, $compareErrorMsg);
}
else {
$formState
->setError($fieldElement);
}
}
}