From e51f543c5df09733ee1eab2c9c0b9c18437876c5 Mon Sep 17 00:00:00 2001 From: Sergei Maertens Date: Mon, 8 Apr 2024 17:14:37 +0200 Subject: [PATCH] :bug: [#4126] Conditions may refer to form fields outside the editgrid --- src/openforms/formio/components/vanilla.py | 2 +- .../formio/tests/validation/test_editgrid.py | 58 +++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/src/openforms/formio/components/vanilla.py b/src/openforms/formio/components/vanilla.py index cab5e5372a..68eee42570 100644 --- a/src/openforms/formio/components/vanilla.py +++ b/src/openforms/formio/components/vanilla.py @@ -651,7 +651,7 @@ def run_child_validation(self, data): # configuration and apply the dynamic hide/visible logic to it. # Note that we add the editgrid key as container so that the # conditional.when values resolve, as these look like `editgridparent.child`. - data = FormioData({self.field_name: item}).data + data = FormioData({**self.root.initial_data, self.field_name: item}).data nested_serializer = self._build_child(data=data) try: result.append(nested_serializer.run_validation(item)) diff --git a/src/openforms/formio/tests/validation/test_editgrid.py b/src/openforms/formio/tests/validation/test_editgrid.py index ca456028df..19190aa959 100644 --- a/src/openforms/formio/tests/validation/test_editgrid.py +++ b/src/openforms/formio/tests/validation/test_editgrid.py @@ -350,6 +350,64 @@ def test_conditional_hidden_inside_editgrid(self): self.assertTrue(is_valid) + @tag("dh-673") + def test_conditional_hidden_inside_editgrid_with_reference_to_outside_editgrid( + self, + ): + editgrid: EditGridComponent = { + "type": "editgrid", + "key": "editgrid", + "label": "Edit grid with nested conditional", + "components": [ + { + "type": "textfield", + "key": "nestedInEditgrid", + "label": "Nested in edit grid", + "validate": {"required": True}, + "conditional": { # type: ignore + "eq": "SHOW_NESTED", + "show": True, + "when": "outsideEditgrid", + }, + }, + ], + } + component: FieldsetComponent = { + "type": "fieldset", + "key": "fieldset", + "label": "Container", + "components": [ + { + "type": "textfield", + "key": "outsideEditgrid", + "label": "Textfield outside edit grid", + }, + editgrid, + ], + } + + with self.subTest("invalid"): + invalid_data: JSONValue = { + "outsideEditgrid": "SHOW_NESTED", + "editgrid": [{}], + } + + is_valid, errors = validate_formio_data(component, invalid_data) + + self.assertFalse(is_valid) + error = extract_error(errors["editgrid"][0], "nestedInEditgrid") + self.assertEqual(error.code, "required") + + with self.subTest("valid"): + invalid_data: JSONValue = { + "outsideEditgrid": "NO_SHOW_NESTED", + "editgrid": [{}], + } + + is_valid, errors = validate_formio_data(component, invalid_data) + + self.assertTrue(is_valid) + class EditGridFieldTests(SimpleTestCase): """