From 6b78601b3cb6a96a5e83d8cc85e829c1a3f477d9 Mon Sep 17 00:00:00 2001 From: Kumi Date: Sun, 23 Jun 2024 10:24:18 +0200 Subject: [PATCH] feat(forms): introduce validation and error display Added custom validation methods to ensure input integrity: - `clean_suffix` in `PrefixForm` and `SuffixForm` now enforces numeric input with minimum digit length. - `clean_identifier` in `IdentifierForm` restricts input to alphanumeric or specific special characters. Updated templates to display form errors, enhancing user feedback and experience. --- freedoi/resolver/forms.py | 18 ++++++++++++++++++ .../templates/resolver/identifier_form.html | 5 +++++ .../templates/resolver/prefix_form.html | 5 +++++ .../templates/resolver/suffix_form.html | 5 +++++ 4 files changed, 33 insertions(+) diff --git a/freedoi/resolver/forms.py b/freedoi/resolver/forms.py index 822355c..26da0df 100644 --- a/freedoi/resolver/forms.py +++ b/freedoi/resolver/forms.py @@ -14,6 +14,12 @@ class PrefixForm(forms.ModelForm): if self.instance.pk: self.fields["prefix"].widget.attrs["readonly"] = True + def clean_suffix(self): + suffix = self.cleaned_data['suffix'] + if not suffix.isdigit() or len(suffix) < 2: + raise forms.ValidationError('Suffix must be numeric and at least four digits long.') + return suffix + class SuffixForm(forms.ModelForm): class Meta: @@ -32,6 +38,12 @@ class SuffixForm(forms.ModelForm): self.fields["suffix"].widget.attrs["readonly"] = True self.fields["prefix"].widget.attrs["readonly"] = True + def clean_suffix(self): + suffix = self.cleaned_data['suffix'] + if not suffix.isdigit() or len(suffix) < 4: + raise forms.ValidationError('Suffix must be numeric and at least four digits long.') + return suffix + class SuffixApprovalForm(forms.ModelForm): class Meta: @@ -52,6 +64,12 @@ class IdentifierForm(forms.ModelForm): self.fields["identifier"].widget.attrs["readonly"] = True self.fields["suffix"].widget.attrs["readonly"] = True + def clean_identifier(self): + identifier = self.cleaned_data['identifier'] + if not all(c.isalnum() or c in '/-' for c in identifier): + raise forms.ValidationError('Identifier must be alphanumeric or contain / or - characters only.') + return identifier + class PermissionForm(forms.ModelForm): class Meta: diff --git a/freedoi/resolver/templates/resolver/identifier_form.html b/freedoi/resolver/templates/resolver/identifier_form.html index c5d7564..562b314 100644 --- a/freedoi/resolver/templates/resolver/identifier_form.html +++ b/freedoi/resolver/templates/resolver/identifier_form.html @@ -4,6 +4,11 @@

{% if form.instance.pk %}Edit{% else %}Create{% endif %} Identifier

+ {% if form.errors %} + + {% endif %} {% csrf_token %} {{ form | crispy }}
diff --git a/freedoi/resolver/templates/resolver/prefix_form.html b/freedoi/resolver/templates/resolver/prefix_form.html index 9c752d0..6720d0c 100644 --- a/freedoi/resolver/templates/resolver/prefix_form.html +++ b/freedoi/resolver/templates/resolver/prefix_form.html @@ -3,6 +3,11 @@ {% load crispy_forms_tags %} {% block content %}

{% if form.instance.pk %}Edit{% else %}Create{% endif %} Prefix

+ {% if form.errors %} + + {% endif %} {% csrf_token %} {{ form | crispy }}
diff --git a/freedoi/resolver/templates/resolver/suffix_form.html b/freedoi/resolver/templates/resolver/suffix_form.html index 1085748..43e1d3a 100644 --- a/freedoi/resolver/templates/resolver/suffix_form.html +++ b/freedoi/resolver/templates/resolver/suffix_form.html @@ -4,6 +4,11 @@

{% if form.instance.pk %}Edit{% else %}Create{% endif %} Suffix

+ {% if form.errors %} + + {% endif %} {% csrf_token %} {{ form | crispy }}

Please make sure to include a full description of your intended use of the suffix in the "Description" field. Your request