Skip to content

Commit

Permalink
Clearing server-side validation errors upon submit (#1407)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Chandra Lash authored and amplifi committed Apr 24, 2017
1 parent 7036e1c commit 6afd04a
Show file tree
Hide file tree
Showing 20 changed files with 28 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ $(document).ready(function() {
if (formIsValid) {
button.form.submit();
button.disabled = true;
} else {
$(".error-block").html(""); // removes previous django validation errors
};
});

});
3 changes: 3 additions & 0 deletions cadasta/organization/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,16 @@ 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']
invalid_names = settings.CADASTA_INVALID_ENTITY_NAMES
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):
Expand Down
2 changes: 1 addition & 1 deletion cadasta/organization/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions cadasta/templates/organization/organization_add.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{% extends "organization/organization_list.html" %}

{% load i18n %}
{% load staticfiles %}

{% block title %} | {% trans "Add organization" %}{% endblock %}

Expand Down Expand Up @@ -39,4 +40,5 @@ <h3 class="modal-title">{% trans "Add organization" %}</h3>

{% block extra_script %}
{{ form.media }}
<script src="{% static 'js/form_submit.js' %}"></script>
{% endblock %}
2 changes: 2 additions & 0 deletions cadasta/templates/organization/organization_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

{% load i18n %}
{% load widget_tweaks %}
{% load staticfiles %}

{% block title %} | {% trans "Edit" %} {{ form.instance.name }}{% endblock %}

Expand Down Expand Up @@ -41,4 +42,5 @@ <h3 class="modal-title">{% trans "Edit organization" %}</h3>

{% block extra_script %}
{{ form.media }}
<script src="{% static 'js/form_submit.js' %}"></script>
{% endblock %}
4 changes: 3 additions & 1 deletion cadasta/templates/organization/project_add_details.html
Original file line number Diff line number Diff line change
@@ -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 }}
<script src="{% static 'js/form_submit.js' %}"></script>
<script>
$(document).ready(function () {
var logos = {{ org_logos|safe }};
Expand Down
1 change: 1 addition & 0 deletions cadasta/templates/organization/project_edit_details.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
{% block extra_script %}
{{ block.super }}
{{ form.media }}
<script src="{% static 'js/form_submit.js' %}"></script>
{% endblock %}

{% block page_title %}{% trans "Edit project details" %} | {% endblock %}
Expand Down
2 changes: 1 addition & 1 deletion cadasta/templates/party/party_add.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
{% block extra_script %}
{{ block.super }}
<script src="{% static 'js/jquery-ui.min.js' %}"></script>
<script src="{% static 'js/form_submit_once.js' %}"></script>
<script src="{% static 'js/form_submit.js' %}"></script>
{% if get_current_language != "en-us" %}
{% get_current_language as LANGUAGE_CODE %}
<script src="https://cdn.rawgit.com/jquery/jquery-ui/1.12.1/ui/i18n/datepicker-{{ LANGUAGE_CODE }}.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion cadasta/templates/party/party_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
{{ block.super }}
{{ form.media }}
<script src="{% static 'js/jquery-ui.min.js' %}"></script>
<script src="{% static 'js/form_submit_once.js' %}"></script>
<script src="{% static 'js/form_submit.js' %}"></script>
{% if get_current_language != "en-us" %}
{% get_current_language as LANGUAGE_CODE %}
<script src="https://cdn.rawgit.com/jquery/jquery-ui/1.12.1/ui/i18n/datepicker-{{ LANGUAGE_CODE }}.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion cadasta/templates/party/relationship_resources_add.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

{% block location_extra_script %}
<script src="{% static 'js/dataTables.forms.js' %}"></script>
<script src="{% static 'js/form_submit_once.js' %}"></script>
<script src="{% static 'js/form_submit.js' %}"></script>
{% include 'resources/script_add_lib.html' %}
{% endblock %}

Expand Down
2 changes: 1 addition & 1 deletion cadasta/templates/party/relationship_resources_new.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

{% block location_extra_script %}
{{ form.media }}
<script src="{% static 'js/form_submit_once.js' %}"></script>
<script src="{% static 'js/form_submit.js' %}"></script>
{% endblock %}

{% block modals %}
Expand Down
2 changes: 1 addition & 1 deletion cadasta/templates/party/resources_add.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{% block extra_script %}
{{ block.super }}
<script src="{% static 'js/dataTables.forms.js' %}"></script>
<script src="{% static 'js/form_submit_once.js' %}"></script>
<script src="{% static 'js/form_submit.js' %}"></script>
{% include 'resources/script_add_lib.html' %}
{% endblock %}

Expand Down
2 changes: 1 addition & 1 deletion cadasta/templates/party/resources_new.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{% block extra_script %}
{{ block.super }}
{{ form.media }}
<script src="{% static 'js/form_submit_once.js' %}"></script>
<script src="{% static 'js/form_submit.js' %}"></script>
{% endblock %}

{% block modals %}
Expand Down
2 changes: 1 addition & 1 deletion cadasta/templates/resources/project_add_existing.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{% block extra_script %}
{{ block.super }}
<script src="{% static 'js/dataTables.forms.js' %}"></script>
<script src="{% static 'js/form_submit_once.js' %}"></script>
<script src="{% static 'js/form_submit.js' %}"></script>
{% include 'resources/script_add_lib.html' %}
{% endblock %}

Expand Down
2 changes: 1 addition & 1 deletion cadasta/templates/resources/project_add_new.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{% block extra_script %}
{{ block.super }}
{{ form.media }}
<script src="{% static 'js/form_submit_once.js' %}"></script>
<script src="{% static 'js/form_submit.js' %}"></script>
{% endblock %}

{% block modals %}
Expand Down
2 changes: 1 addition & 1 deletion cadasta/templates/spatial/location_add.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<script src="https://cdn.rawgit.com/jquery/jquery-ui/1.12.1/ui/i18n/datepicker-{{ LANGUAGE_CODE }}.js"></script>
{% endif %}
<script src="{% static 'js/map_utils.js' %}"></script>
<script src="{% static 'js/form_submit_once.js' %}"></script>
<script src="{% static 'js/form_submit.js' %}"></script>
<script>
$(document).ready(function () {
$(window).on('map:init', function(e) {
Expand Down
2 changes: 1 addition & 1 deletion cadasta/templates/spatial/location_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<script src="https://cdn.rawgit.com/jquery/jquery-ui/1.12.1/ui/i18n/datepicker-{{ LANGUAGE_CODE }}.js"></script>
{% endif %}
<script src="{% static 'js/map_utils.js' %}"></script>
<script src="{% static 'js/form_submit_once.js' %}"></script>
<script src="{% static 'js/form_submit.js' %}"></script>
<script>
$(document).ready(function () {
$(window).on('map:init', function(e) {
Expand Down
2 changes: 1 addition & 1 deletion cadasta/templates/spatial/relationship_add.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
{% block location_extra_script %}
{{ form.media }}
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<script src="{% static 'js/form_submit_once.js' %}"></script>
<script src="{% static 'js/form_submit.js' %}"></script>
<script src="{% static 'js/jquery-ui.min.js' %}"></script>
{% if get_current_language != "en-us" %}
{% get_current_language as LANGUAGE_CODE %}
Expand Down
2 changes: 1 addition & 1 deletion cadasta/templates/spatial/resources_add.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

{% block location_extra_script %}
<script src="{% static 'js/dataTables.forms.js' %}"></script>
<script src="{% static 'js/form_submit_once.js' %}"></script>
<script src="{% static 'js/form_submit.js' %}"></script>
{% include 'resources/script_add_lib.html' %}
{% endblock %}

Expand Down
2 changes: 1 addition & 1 deletion cadasta/templates/spatial/resources_new.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

{% block location_extra_script %}
{{ form.media }}
<script src="{% static 'js/form_submit_once.js' %}"></script>
<script src="{% static 'js/form_submit.js' %}"></script>
{% endblock %}

{% block modals %}
Expand Down

0 comments on commit 6afd04a

Please sign in to comment.