Repository: ocadotechnology/codeforlife-portal

Issue:
Postcode / Zipcode error message at School creation is misleading
Since most web browsers autocomplete the postcode in the class creation form as the email address, teachers receive a badly worded AND badly positioned error message (the message makes it sound like the error is in the name)
It should mention something like, please input a valid postcode / zipcode 
and be below the postcode field.


Discussion:

Resolving patch:
diff --git a/portal/forms/organisation.py b/portal/forms/organisation.py
--- a/portal/forms/organisation.py
+++ b/portal/forms/organisation.py
@@ -52,14 +52,14 @@ class Meta:
         model = School
         fields = ['name', 'postcode', 'country']
         labels = {
-            'name' : "Name of your school or club",
-            'postcode' : 'Postcode',
-            'country' : 'Country',
+            'name': "Name of your school or club",
+            'postcode': 'Postcode',
+            'country': 'Country',
             }
         widgets = {
-            'name' : forms.TextInput(attrs={'autocomplete': "off", 'placeholder': 'Name of your school or club'}),
-            'postcode' : forms.TextInput(attrs={'autocomplete': "off", 'placeholder': 'Postcode'}),
-            'country' : CountrySelectWidget(attrs={'class': 'wide'}),
+            'name': forms.TextInput(attrs={'autocomplete': "off", 'placeholder': 'Name of your school or club'}),
+            'postcode': forms.TextInput(attrs={'autocomplete': "off", 'placeholder': 'Postcode'}),
+            'country': CountrySelectWidget(attrs={'class': 'wide'}),
             }
 
     def __init__(self, *args, **kwargs):
@@ -89,9 +89,8 @@ def clean_postcode(self):
         postcode = self.cleaned_data.get('postcode', None)
 
         if postcode:
-            # Basic postcode check for now
-            if not len(postcode.replace(' ', '')) > 0:
-                raise forms.ValidationError("That postcode was not recognised")
+            if len(postcode.replace(' ', '')) > 10 or len(postcode.replace(' ', '')) == 0:
+                raise forms.ValidationError("Please enter a valid postcode or ZIP code")
 
         return postcode
 


Test patch:
diff --git a/portal/tests/pageObjects/portal/teach/onboarding_organisation_page.py b/portal/tests/pageObjects/portal/teach/onboarding_organisation_page.py
--- a/portal/tests/pageObjects/portal/teach/onboarding_organisation_page.py
+++ b/portal/tests/pageObjects/portal/teach/onboarding_organisation_page.py
@@ -96,6 +96,11 @@ def has_creation_failed(self):
         error = 'There is already a school or club registered with that name and postcode'
         return error in errors
 
+    def was_postcode_invalid(self):
+        errors = self.browser.find_element_by_id('form-create-organisation').find_element_by_class_name('errorlist').text
+        error = 'Please enter a valid postcode or ZIP code'
+        return error in errors
+
     def join_organisation(self, name):
         self.browser.find_element_by_id('join-tab').click()
         self.browser.find_element_by_id('id_fuzzy_name').send_keys(name)
diff --git a/portal/tests/test_organisation.py b/portal/tests/test_organisation.py
--- a/portal/tests/test_organisation.py
+++ b/portal/tests/test_organisation.py
@@ -96,6 +96,15 @@ def test_create_clash(self):
 
         assert page.has_creation_failed()
 
+    def test_create_invalid_postcode(self):
+        email, password = signup_teacher_directly()
+
+        selenium.get(self.live_server_url)
+        page = HomePage(selenium).go_to_login_page().login_no_school(email, password)
+
+        page = page.create_organisation_failure('School', password, '   ')
+        assert page.was_postcode_invalid()
+
     def test_revoke(self):
         email_1, password_1 = signup_teacher_directly()
         email_2, password_2 = signup_teacher_directly()