From 6afd04a4842cb51b20da2d7fe6bfc273a4ef7ab9 Mon Sep 17 00:00:00 2001 From: Chandra Lash Date: Mon, 24 Apr 2017 01:43:21 -0500 Subject: [PATCH] Clearing server-side validation errors upon submit (#1407) * Adding validation to translate to org duplicates Change text Add script to remove error block on submit if form not valid Removed alert Changed form_submit_once to form_submit * Moving duplicate error message to init --- .../core/static/js/{form_submit_once.js => form_submit.js} | 3 +++ cadasta/organization/forms.py | 3 +++ cadasta/organization/tests/test_forms.py | 2 +- cadasta/templates/organization/organization_add.html | 2 ++ cadasta/templates/organization/organization_edit.html | 2 ++ cadasta/templates/organization/project_add_details.html | 4 +++- cadasta/templates/organization/project_edit_details.html | 1 + cadasta/templates/party/party_add.html | 2 +- cadasta/templates/party/party_edit.html | 2 +- cadasta/templates/party/relationship_resources_add.html | 2 +- cadasta/templates/party/relationship_resources_new.html | 2 +- cadasta/templates/party/resources_add.html | 2 +- cadasta/templates/party/resources_new.html | 2 +- cadasta/templates/resources/project_add_existing.html | 2 +- cadasta/templates/resources/project_add_new.html | 2 +- cadasta/templates/spatial/location_add.html | 2 +- cadasta/templates/spatial/location_edit.html | 2 +- cadasta/templates/spatial/relationship_add.html | 2 +- cadasta/templates/spatial/resources_add.html | 2 +- cadasta/templates/spatial/resources_new.html | 2 +- 20 files changed, 28 insertions(+), 15 deletions(-) rename cadasta/core/static/js/{form_submit_once.js => form_submit.js} (78%) diff --git a/cadasta/core/static/js/form_submit_once.js b/cadasta/core/static/js/form_submit.js similarity index 78% rename from cadasta/core/static/js/form_submit_once.js rename to cadasta/core/static/js/form_submit.js index c3b9fa793..2cdd4c886 100644 --- a/cadasta/core/static/js/form_submit_once.js +++ b/cadasta/core/static/js/form_submit.js @@ -8,6 +8,9 @@ $(document).ready(function() { if (formIsValid) { button.form.submit(); button.disabled = true; + } else { + $(".error-block").html(""); // removes previous django validation errors }; }); + }); diff --git a/cadasta/organization/forms.py b/cadasta/organization/forms.py index 8f08057e9..54a20820a 100644 --- a/cadasta/organization/forms.py +++ b/cadasta/organization/forms.py @@ -128,6 +128,8 @@ class Meta: def __init__(self, *args, **kwargs): self.user = kwargs.pop('user', None) super().__init__(*args, **kwargs) + self.fields['name'].error_messages['unique'] = _( + "Organization with this name already exists.") def clean_name(self): name = self.cleaned_data['name'] @@ -135,6 +137,7 @@ def clean_name(self): if slugify(name, allow_unicode=True) in invalid_names: raise forms.ValidationError( _("Organization name cannot be “Add” or “New”.")) + return name def save(self, *args, **kwargs): diff --git a/cadasta/organization/tests/test_forms.py b/cadasta/organization/tests/test_forms.py index 3d75dac16..c75a48d8c 100644 --- a/cadasta/organization/tests/test_forms.py +++ b/cadasta/organization/tests/test_forms.py @@ -65,7 +65,7 @@ def test_duplicate_name_error(self): self.data['description'] = 'Org description #2' form = forms.OrganizationForm(self.data, user=UserFactory.create()) assert form.is_valid() is False - assert ("Organization with this Name already exists." + assert ("Organization with this name already exists." in form.errors['name']) assert Organization.objects.count() == 1 diff --git a/cadasta/templates/organization/organization_add.html b/cadasta/templates/organization/organization_add.html index 71998a3e6..e2430f88b 100644 --- a/cadasta/templates/organization/organization_add.html +++ b/cadasta/templates/organization/organization_add.html @@ -1,6 +1,7 @@ {% extends "organization/organization_list.html" %} {% load i18n %} +{% load staticfiles %} {% block title %} | {% trans "Add organization" %}{% endblock %} @@ -39,4 +40,5 @@ {% block extra_script %} {{ form.media }} + {% endblock %} diff --git a/cadasta/templates/organization/organization_edit.html b/cadasta/templates/organization/organization_edit.html index 05426ce12..048d54f13 100644 --- a/cadasta/templates/organization/organization_edit.html +++ b/cadasta/templates/organization/organization_edit.html @@ -2,6 +2,7 @@ {% load i18n %} {% load widget_tweaks %} +{% load staticfiles %} {% block title %} | {% trans "Edit" %} {{ form.instance.name }}{% endblock %} @@ -41,4 +42,5 @@ {% block extra_script %} {{ form.media }} + {% endblock %} diff --git a/cadasta/templates/organization/project_add_details.html b/cadasta/templates/organization/project_add_details.html index ff3cd6c0f..a661b2252 100644 --- a/cadasta/templates/organization/project_add_details.html +++ b/cadasta/templates/organization/project_add_details.html @@ -1,12 +1,14 @@ {% extends "organization/project_add_wrapper.html" %} -{% load widget_tweaks %} +{% load staticfiles %} {% load i18n %} +{% load widget_tweaks %} {% block page_title %}| {% trans "Add details" %}{% endblock %} {% block extra_script %} {{ form.media }} + {% endblock %} {% block page_title %}{% trans "Edit project details" %} | {% endblock %} diff --git a/cadasta/templates/party/party_add.html b/cadasta/templates/party/party_add.html index e0ccc3743..337e1c8c0 100644 --- a/cadasta/templates/party/party_add.html +++ b/cadasta/templates/party/party_add.html @@ -12,7 +12,7 @@ {% block extra_script %} {{ block.super }} - + {% if get_current_language != "en-us" %} {% get_current_language as LANGUAGE_CODE %} diff --git a/cadasta/templates/party/party_edit.html b/cadasta/templates/party/party_edit.html index a9f1bc813..2833dfa85 100644 --- a/cadasta/templates/party/party_edit.html +++ b/cadasta/templates/party/party_edit.html @@ -13,7 +13,7 @@ {{ block.super }} {{ form.media }} - + {% if get_current_language != "en-us" %} {% get_current_language as LANGUAGE_CODE %} diff --git a/cadasta/templates/party/relationship_resources_add.html b/cadasta/templates/party/relationship_resources_add.html index bcdd0d04b..714cd9476 100644 --- a/cadasta/templates/party/relationship_resources_add.html +++ b/cadasta/templates/party/relationship_resources_add.html @@ -6,7 +6,7 @@ {% block location_extra_script %} - + {% include 'resources/script_add_lib.html' %} {% endblock %} diff --git a/cadasta/templates/party/relationship_resources_new.html b/cadasta/templates/party/relationship_resources_new.html index f47661347..b1b8fe902 100644 --- a/cadasta/templates/party/relationship_resources_new.html +++ b/cadasta/templates/party/relationship_resources_new.html @@ -6,7 +6,7 @@ {% block location_extra_script %} {{ form.media }} - + {% endblock %} {% block modals %} diff --git a/cadasta/templates/party/resources_add.html b/cadasta/templates/party/resources_add.html index 110a0d11b..90328974a 100644 --- a/cadasta/templates/party/resources_add.html +++ b/cadasta/templates/party/resources_add.html @@ -7,7 +7,7 @@ {% block extra_script %} {{ block.super }} - + {% include 'resources/script_add_lib.html' %} {% endblock %} diff --git a/cadasta/templates/party/resources_new.html b/cadasta/templates/party/resources_new.html index 507e90adb..408c5f4de 100644 --- a/cadasta/templates/party/resources_new.html +++ b/cadasta/templates/party/resources_new.html @@ -7,7 +7,7 @@ {% block extra_script %} {{ block.super }} {{ form.media }} - + {% endblock %} {% block modals %} diff --git a/cadasta/templates/resources/project_add_existing.html b/cadasta/templates/resources/project_add_existing.html index afcaf3ee6..bc701b85c 100644 --- a/cadasta/templates/resources/project_add_existing.html +++ b/cadasta/templates/resources/project_add_existing.html @@ -7,7 +7,7 @@ {% block extra_script %} {{ block.super }} - + {% include 'resources/script_add_lib.html' %} {% endblock %} diff --git a/cadasta/templates/resources/project_add_new.html b/cadasta/templates/resources/project_add_new.html index 5fe5c0eaf..781d43d72 100644 --- a/cadasta/templates/resources/project_add_new.html +++ b/cadasta/templates/resources/project_add_new.html @@ -7,7 +7,7 @@ {% block extra_script %} {{ block.super }} {{ form.media }} - + {% endblock %} {% block modals %} diff --git a/cadasta/templates/spatial/location_add.html b/cadasta/templates/spatial/location_add.html index c309482b1..d700b6ccc 100644 --- a/cadasta/templates/spatial/location_add.html +++ b/cadasta/templates/spatial/location_add.html @@ -32,7 +32,7 @@ {% endif %} - + {% endif %} - + - + {% if get_current_language != "en-us" %} {% get_current_language as LANGUAGE_CODE %} diff --git a/cadasta/templates/spatial/resources_add.html b/cadasta/templates/spatial/resources_add.html index 8b565d3e6..eeaddcb40 100644 --- a/cadasta/templates/spatial/resources_add.html +++ b/cadasta/templates/spatial/resources_add.html @@ -6,7 +6,7 @@ {% block location_extra_script %} - + {% include 'resources/script_add_lib.html' %} {% endblock %} diff --git a/cadasta/templates/spatial/resources_new.html b/cadasta/templates/spatial/resources_new.html index f32c5f861..2a04b5b7a 100644 --- a/cadasta/templates/spatial/resources_new.html +++ b/cadasta/templates/spatial/resources_new.html @@ -6,7 +6,7 @@ {% block location_extra_script %} {{ form.media }} - + {% endblock %} {% block modals %}