diff --git a/judgments/forms/fields.py b/judgments/forms/fields.py index 5aec676db..754e5be96 100644 --- a/judgments/forms/fields.py +++ b/judgments/forms/fields.py @@ -4,6 +4,7 @@ from crispy_forms_gds.fields import DateInputField from django import forms +from django.core.exceptions import ValidationError from django.core.validators import RegexValidator from .validators import ValidateYearRange @@ -80,9 +81,14 @@ def compress(self, data_list): month = 1 if not day: day = 1 - if self.date_type == "to": + elif self.date_type == "to": if not month: month = 12 if not day: day = monthrange(year, month)[1] - return date(year=year, month=month, day=day) # type: ignore + else: + raise RuntimeError("date_type is neither `from` nor `to`") + try: + return date(day=day, month=month, year=year) + except ValueError as e: + raise ValidationError(str(e)) from e