Repository: svthalia/concrexit

Issue:
Strip spaces in phone number inputs
### Is your feature request related to a problem? Please describe.
During registration, a sjaars had issues where a phone number didn't validate. It runed out the problem was they had spaces between groups of digits.

### Describe the solution you'd like
Spaces can be removed before validation or ignored some other way.

### Motivation
Better UX.




Discussion:

Resolving patch:
diff --git a/website/members/forms.py b/website/members/forms.py
--- a/website/members/forms.py
+++ b/website/members/forms.py
@@ -79,6 +79,18 @@ def save(self, commit=True):
 
         return instance
 
+    def clean(self):
+        if "phone_number" in self.cleaned_data:
+            self.cleaned_data["phone_number"] = self.cleaned_data[
+                "phone_number"
+            ].replace(" ", "")
+
+        if "emergency_contact_phone_number" in self.cleaned_data:
+            self.cleaned_data["emergency_contact_phone_number"] = self.cleaned_data[
+                "emergency_contact_phone_number"
+            ].replace(" ", "")
+        super().clean()
+
 
 class UserCreationForm(BaseUserCreationForm):
     """Custom Form that lowercases the username on creation."""
@@ -86,6 +98,7 @@ class UserCreationForm(BaseUserCreationForm):
     def clean(self):
         if "username" in self.cleaned_data:
             self.cleaned_data["username"] = self.cleaned_data["username"].lower()
+
         super().clean()
 
     class Meta:
diff --git a/website/registrations/forms.py b/website/registrations/forms.py
--- a/website/registrations/forms.py
+++ b/website/registrations/forms.py
@@ -43,6 +43,13 @@ def __init__(self, *args, **kwargs):
         )
         self.fields["birthday"].widget.input_type = "date"
 
+    def clean(self):
+        if "phone_number" in self.cleaned_data:  # pragma: no cover
+            self.cleaned_data["phone_number"] = self.cleaned_data[
+                "phone_number"
+            ].replace(" ", "")
+        super().clean()
+
 
 class RegistrationAdminForm(forms.ModelForm):
     """Custom admin form for Registration model to add the widget for the signature."""


Test patch:
diff --git a/website/registrations/tests/test_forms.py b/website/registrations/tests/test_forms.py
--- a/website/registrations/tests/test_forms.py
+++ b/website/registrations/tests/test_forms.py
@@ -22,7 +22,7 @@ def setUp(self):
             "address_postal_code": "6525AJ",
             "address_city": "Nijmegen",
             "address_country": "NL",
-            "phone_number": "06123456789",
+            "phone_number": "06 12345678",
             "birthday": timezone.now().replace(year=1990, day=1),
             "language": "en",
             "length": Entry.MEMBERSHIP_YEAR,
@@ -34,6 +34,7 @@ def test_privacy_policy_checked(self):
         with self.subTest("Form is valid"):
             form = forms.MemberRegistrationForm(self.data)
             self.assertTrue(form.is_valid(), msg=dict(form.errors))
+            self.assertEqual(form.cleaned_data["phone_number"], "0612345678")
         with self.subTest("Form is not valid"):
             self.data["privacy_policy"] = 0
             form = forms.MemberRegistrationForm(self.data)