From b0263491a0eece0c42664d554f7fc6ba6637c275 Mon Sep 17 00:00:00 2001 From: Ben Sturmfels Date: Fri, 10 Dec 2021 18:37:13 +1100 Subject: [PATCH] assignment: Validate that end date is provided if you didn't choose open-ended. --- www/conservancy/apps/assignment/forms.py | 3 +++ .../templates/assignment/assignment_form.html | 9 ++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/www/conservancy/apps/assignment/forms.py b/www/conservancy/apps/assignment/forms.py index 8777e0b4..288dc75a 100644 --- a/www/conservancy/apps/assignment/forms.py +++ b/www/conservancy/apps/assignment/forms.py @@ -62,3 +62,6 @@ class AssignmentForm(forms.ModelForm): def clean_period_ends(self): if 'period_begins' in self.cleaned_data and 'period_ends' in self.cleaned_data and self.cleaned_data['period_begins'] and self.cleaned_data['period_ends'] and self.cleaned_data['period_begins'] > self.cleaned_data['period_ends']: raise ValidationError('End of period is before start') + + if self.cleaned_data['period_end_type'] == 'a specific past date' and not self.cleaned_data['period_ends']: + raise ValidationError('This field is required') diff --git a/www/conservancy/templates/assignment/assignment_form.html b/www/conservancy/templates/assignment/assignment_form.html index ea13b9a1..d7c2cc1a 100644 --- a/www/conservancy/templates/assignment/assignment_form.html +++ b/www/conservancy/templates/assignment/assignment_form.html @@ -24,7 +24,8 @@ // End date field should be shown only when "a specific past date" is selected. const form = document.querySelector('#assignment-form'); const past_date_label = document.querySelector('label[for=id_period_ends]'); - const past_date_field = document.querySelector('#assignment-form > p:nth-child(11)'); + const past_date_field = document.querySelector('#id_period_ends'); + const past_date_container = past_date_field.parentElement; form.addEventListener('change', togglePastDate); togglePastDate(); // Run change handler once to initialise form. @@ -33,10 +34,12 @@ function togglePastDate() { if (form['period_end_type'].value === 'all future contributions') { - past_date_field.style.display = 'none'; + past_date_container.style.display = 'none'; + past_date_field.required = false; } else { - past_date_field.style.display = ''; + past_date_container.style.display = ''; + past_date_field.required = true; } }