diff --git a/src/Element/WebformRadiosOther.php b/src/Element/WebformRadiosOther.php index 805b593af..3360aed5f 100644 --- a/src/Element/WebformRadiosOther.php +++ b/src/Element/WebformRadiosOther.php @@ -2,6 +2,8 @@ namespace Drupal\webform\Element; +use Drupal\Core\Form\FormStateInterface; + /** * Provides a webform element for radio buttons with an other option. * @@ -14,4 +16,27 @@ class WebformRadiosOther extends WebformOtherBase { */ protected static $type = 'radios'; + /** + * {@inheritdoc} + */ + public static function valueCallback(&$element, $input, FormStateInterface $form_state) { + // Remove 'webform_' prefix from type. + $type = str_replace('webform_', '', static::$type); + + // Unset #default_value that is not a valid #option. + // + // This behavior is needed when a radios #default_value was previously set + // but now the radios are unchecked via conditional logic. This results in + // nothing being posted back to the server, and the #default_value is used + // which throws "An illegal choice has been detected." error. + if ($input + && !isset($input[$type]) + && isset($element['#default_value']) + && !isset($element['#option'][$element['#default_value']])) { + unset($element['#default_value']); + } + + return parent::valueCallback($element, $input, $form_state); + } + }