You are here

public static function Checkboxes::validateCheckAllOrNone in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformElement/Checkboxes.php \Drupal\webform\Plugin\WebformElement\Checkboxes::validateCheckAllOrNone()

Form API callback. Handle check all or none option.

File

src/Plugin/WebformElement/Checkboxes.php, line 120

Class

Checkboxes
Provides a 'checkboxes' element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

public static function validateCheckAllOrNone(array &$element, FormStateInterface $form_state, array &$completed_form) {
  $values = $form_state
    ->getValue($element['#parents'], []);
  if (!empty($element['#options_all'])) {

    // Remove options all value.
    WebformArrayHelper::removeValue($values, $element['#options_all_value']);
  }
  elseif (!empty($element['#options_none']) && in_array($element['#options_none_value'], $values)) {

    // Only allow none option to be submitted.
    $values = [
      $element['#options_none_value'],
    ];
  }
  $form_state
    ->setValueForElement($element, $values);
}