Skip to content

Commit 5407a96

Browse files
committed
Handle Django CharField form field from stripping values during validation.
1 parent 144bb56 commit 5407a96

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

django_unicorn/components.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,15 @@ def get_frontend_context_variables(self) -> str:
310310
if key in form.cleaned_data:
311311
cleaned_value = form.cleaned_data[key]
312312
value = field.widget.format_value(cleaned_value)
313-
frontend_context_variables[key] = value
313+
314+
# Don't update the frontend variable if the only change is
315+
# stripping off the whitespace from the field value
316+
# https://docs.djangoproject.com/en/stable/ref/forms/fields/#django.forms.CharField.strip
317+
if (
318+
not hasattr(frontend_context_variables[key], "strip")
319+
or frontend_context_variables[key].strip() != value
320+
):
321+
frontend_context_variables[key] = value
314322

315323
encoded_frontend_context_variables = serializer.dumps(
316324
frontend_context_variables
@@ -423,7 +431,12 @@ def _set_property(self, name, value):
423431
form = self._get_form(data)
424432

425433
if form and name in form.fields and name in form.cleaned_data:
426-
value = form.cleaned_data[name]
434+
# The Django form CharField validator will remove whitespace
435+
# from the field value. Ignore that update if it's the
436+
# only thing different from the validator
437+
# https://docs.djangoproject.com/en/stable/ref/forms/fields/#django.forms.CharField.strip
438+
if not hasattr(value, "strip") or form.cleaned_data[name] != value.strip():
439+
value = form.cleaned_data[name]
427440

428441
updating_function_name = f"updating_{name}"
429442
if hasattr(self, updating_function_name):

0 commit comments

Comments
 (0)