Skip to content

Commit

Permalink
Merge pull request #1071 from rcpch/mbarton/org-audit-validation-errors
Browse files Browse the repository at this point in the history
Show validation errors in UI
  • Loading branch information
mbarton authored Oct 11, 2024
2 parents 86319d1 + 35d6bea commit fdec437
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 29 deletions.
10 changes: 8 additions & 2 deletions epilepsy12/models_folder/organisational_audit.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.core.validators import MaxValueValidator
from django.core.validators import MinValueValidator, MaxValueValidator
from django.contrib.gis.db import models

from simple_history.models import HistoricalRecords
Expand All @@ -11,7 +11,13 @@


def DecimalField():
return models.DecimalField(null=True, blank=True, max_digits=7, decimal_places=3)
return models.DecimalField(
null=True,
blank=True,
max_digits=7,
decimal_places=3,
validators=[MinValueValidator(0)]
)

def TextField():
return models.CharField(null=True, blank=True)
Expand Down
36 changes: 20 additions & 16 deletions epilepsy12/views/organisational_audit_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def group_form_fields(form):
total_questions = 0

for field in form:
if field.value() is not None and field.value() != "" and field.value() != []:
if field.value() is not None and field.value() != "" and field.value() != [] and not field.errors:
completed = True
number_completed += 1
else:
Expand Down Expand Up @@ -172,19 +172,22 @@ def get_submission(submission_period, group, group_field):
).first()


# Avoid saving errors to the DB by reconstructing the form from the data
# Allows us to also mix in answers from the previous years submission
def get_submission_form(submission, last_submission):
submission = submission or OrganisationalAuditSubmission()
data = {}

if last_submission:
for field in OrganisationalAuditSubmission._meta.fields:
if field.name != "id" and not field.name in OrganisationalAuditSubmissionForm.Meta.exclude:
current_field_value = getattr(submission, field.name)
last_field_value = getattr(last_submission, field.name)

if not current_field_value and last_field_value:
setattr(submission, field.name, last_field_value)

return OrganisationalAuditSubmissionForm(instance=submission)
for field in OrganisationalAuditSubmission._meta.fields:
if field.name != "id" and not field.name in OrganisationalAuditSubmissionForm.Meta.exclude:
field_value = getattr(submission, field.name) if submission else None

if not field_value and last_submission:
field_value = getattr(last_submission, field.name)

data[field.name] = field_value


return OrganisationalAuditSubmissionForm(data, instance=submission)


def _organisational_audit(request, group_id, group_model, group_field):
Expand Down Expand Up @@ -231,11 +234,12 @@ def _organisational_audit(request, group_id, group_model, group_field):
context["percentage_completed"] = int(
(number_completed / total_questions) * 100
)
context["form"] = form

if not form.errors:
form.save()
else:
context["errors"] = form.errors
# Validate all the data to pull through the values
form.is_valid()
# but save regardless of errors - we save as we go
form.instance.save()

return render(
request, "epilepsy12/partials/organisational_audit_form.html", context
Expand Down
25 changes: 14 additions & 11 deletions templates/epilepsy12/partials/organisational_audit_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
</div>
</div>

{% if errors %}
{{errors}}
{% endif %}

{% for section, questions in questions_by_section.items %}
<div>
<h3 class="org-audit-section-title">
Expand All @@ -23,14 +19,13 @@ <h3 class="org-audit-section-title">
<div class="org-audit-cell">
<label>
<em>{{parent.question_number}}</em>
<span data-tooltip="Some helpful information will be found here...">
{{parent.label}}
<i class="rcpch question circle icon"></i>
</span>
{{parent.label}}
</label>
<div class="field icon-text">
<span class="inline-icon-text">
{% if parent.completed %}
{% if parent.field.errors %}
<i class="rcpch_red exclamation circle icon"></i>
{% elif parent.completed %}
<span data-tooltip="Scored field">
<i class="rcpch_pink check circle outline icon"></i>
</span>
Expand All @@ -42,6 +37,9 @@ <h3 class="org-audit-section-title">
{{parent.field}}
</span>
</div>
{% if parent.field.errors %}
{{parent.field.errors}}
{% endif %}
</div>
{% if parent.reference %}
<dfn class="org-audit-cell org-audit-reference">
Expand All @@ -66,18 +64,23 @@ <h3 class="org-audit-section-title">
</label>
<div class="field">
<span class="inline-icon-text">
{% if child.completed %}
{% if child.field.errors %}
<i class="rcpch_red exclamation circle icon"></i>
{% elif child.completed %}
<span data-tooltip="Scored field">
<i class="rcpch_pink check circle outline icon"></i>
</span>
{% else %}
{% elif "completed" in child %}
<span data-tooltip="Unscored field">
<i class="rcpch_light_blue dot circle outline icon"></i>
</span>
{% endif %}
{{child.field}}
</span>
</div>
{% if child.field.errors %}
{{child.field.errors}}
{% endif %}
</div>
{% endif %}
{% endfor %}
Expand Down

0 comments on commit fdec437

Please sign in to comment.