From e0bacf113ac2e4631d0a5db2e821b1032fe4e40d Mon Sep 17 00:00:00 2001 From: Viicos <65306057+Viicos@users.noreply.github.com> Date: Thu, 4 Apr 2024 09:31:40 +0200 Subject: [PATCH 1/2] [#4096] Fix validation of hidden radio component --- src/openforms/formio/serializers.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/openforms/formio/serializers.py b/src/openforms/formio/serializers.py index dea899300c..3345465352 100644 --- a/src/openforms/formio/serializers.py +++ b/src/openforms/formio/serializers.py @@ -87,6 +87,9 @@ def _remove_validations_from_field(self, field: serializers.Field) -> None: field.min_length = None field.max_length = None + case serializers.ChoiceField(): + field.allow_blank = True + def _get_required(self) -> bool: return any(field.required for field in self.fields.values()) From 44d161e9470871afe41f8cf03b7426ee7bb12f61 Mon Sep 17 00:00:00 2001 From: Viicos <65306057+Viicos@users.noreply.github.com> Date: Thu, 4 Apr 2024 09:31:56 +0200 Subject: [PATCH 2/2] [#4096] Add regression test for hidden radio component --- .../formio/tests/validation/test_radio.py | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/openforms/formio/tests/validation/test_radio.py b/src/openforms/formio/tests/validation/test_radio.py index e69de29bb2..d21d62e9b1 100644 --- a/src/openforms/formio/tests/validation/test_radio.py +++ b/src/openforms/formio/tests/validation/test_radio.py @@ -0,0 +1,31 @@ +from django.test import SimpleTestCase, tag + +from openforms.typing import JSONObject + +from ...typing import RadioComponent +from .helpers import validate_formio_data + + +class RadioValidationTests(SimpleTestCase): + + @tag("gh-4096") + def test_radio_hidden_required(self): + component: RadioComponent = { + "type": "radio", + "key": "radio", + "label": "Radio", + "values": [ + {"label": "Opt1", "value": "opt1"}, + {"label": "Opt2", "value": "opt2"}, + ], + "hidden": True, + "validate": { + "required": True, + }, + } + + # This happens when `clearOnHide` is `False`: + data: JSONObject = {"radio": ""} + + is_valid, _ = validate_formio_data(component, data) + self.assertTrue(is_valid)