Skip to content

Commit

Permalink
[#4774] Added normalizer method to the the Textfield component
Browse files Browse the repository at this point in the history
Backport-of: #4775
  • Loading branch information
vaszig authored and sergei-maertens committed Oct 22, 2024
1 parent 6ef8160 commit ad2a2f8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/openforms/formio/components/vanilla.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ class Default(BasePlugin):
class TextField(BasePlugin[TextFieldComponent]):
formatter = TextFieldFormatter

@staticmethod
def normalizer(component: Component, value: str) -> str:
if isinstance(value, (int, float)):
return str(value)
return value

def build_serializer_field(
self, component: TextFieldComponent
) -> serializers.CharField | serializers.ListField:
Expand Down
12 changes: 11 additions & 1 deletion src/openforms/formio/tests/test_normalization.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.test import SimpleTestCase
from django.test import SimpleTestCase, tag

from ..service import normalize_value_for_component

Expand Down Expand Up @@ -63,3 +63,13 @@ def test_normalize_unknown_component_type(self):
result = normalize_value_for_component(component, "foo.bar-baz")

self.assertEqual(result, "foo.bar-baz") # unmodified

@tag("gh-4774")
def test_textfield_normalization_non_str(self):
component = {"type": "textfield"}

int_result = normalize_value_for_component(component, 9)
float_result = normalize_value_for_component(component, 9.9)

self.assertEqual(int_result, "9")
self.assertEqual(float_result, "9.9")

0 comments on commit ad2a2f8

Please sign in to comment.