You are here

public static function WebformCheckboxValue::validateWebformCheckboxValue in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Element/WebformCheckboxValue.php \Drupal\webform\Element\WebformCheckboxValue::validateWebformCheckboxValue()

Validates a checkbox value element.

File

src/Element/WebformCheckboxValue.php, line 120

Class

WebformCheckboxValue
Provides a webform element for checking a box before entering a value.

Namespace

Drupal\webform\Element

Code

public static function validateWebformCheckboxValue(&$element, FormStateInterface $form_state, &$complete_form) {
  $value = NestedArray::getValue($form_state
    ->getValues(), $element['#parents']);

  // Always require a value when the checkbox is checked.
  if (!empty($value['checkbox']) && empty($value['value'])) {
    WebformElementHelper::setRequiredError($element['value'], $form_state);
  }

  // If checkbox is not checked then empty the value.
  if (empty($value['checkbox'])) {
    $value['value'] = '';
  }
  $form_state
    ->setValueForElement($element['checkbox'], NULL);
  $form_state
    ->setValueForElement($element['value'], NULL);
  $element['#value'] = $value['value'];
  $form_state
    ->setValueForElement($element, $value['value']);
}