You are here

public static function WebformValidateConstraint::validateBackendComponents in Webform Validation 8

Same name and namespace in other branches
  1. 2.0.x src/Validate/WebformValidateConstraint.php \Drupal\webform_validation\Validate\WebformValidateConstraint::validateBackendComponents()

Validates Backend fields.

Parameters

array $form: The form array.

\Drupal\Core\Form\FormStateInterface $formState: The form state.

File

src/Validate/WebformValidateConstraint.php, line 22

Class

WebformValidateConstraint
Form API callback. Validate element value.

Namespace

Drupal\webform_validation\Validate

Code

public static function validateBackendComponents(array $form, FormStateInterface &$formState) : void {
  $valid = FALSE;
  $values = $formState
    ->cleanValues()
    ->getValues();
  if (!empty($values['properties']['equal']) && $values['properties']['equal'] == '1') {
    $equalComponents = array_filter($values['properties']['equal_components']);
    if (empty($equalComponents)) {
      $formState
        ->setErrorByName('equal_components', 'Please select atleast 1 Equal components.');
    }
  }
  if (!empty($values['properties']['some_of_several']) && $values['properties']['some_of_several'] == '1') {
    $sosComponents = array_filter($values['properties']['some_of_several_components']);
    $countSosComponents = count($sosComponents) + 1;
    if ($countSosComponents <= 1) {
      $formState
        ->setErrorByName('some_of_several_components', 'You need to select at least 2 several components.');
    }
  }
  if (!empty($values['properties']['compare']) && $values['properties']['compare'] == '1') {
    $compareComponents = $values['properties']['compare_components'];
    if (empty($compareComponents)) {
      $formState
        ->setErrorByName('compare_components', 'Please select compare components.');
    }
  }
}