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.
This commit is contained in:
Kumi 2024-06-23 10:24:18 +02:00
parent da929654c0
commit 6b78601b3c
Signed by: kumi
GPG key ID: ECBCC9082395383F
4 changed files with 33 additions and 0 deletions

View file

@ -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:

View file

@ -4,6 +4,11 @@
<div class="container mt-5">
<h1>{% if form.instance.pk %}Edit{% else %}Create{% endif %} Identifier</h1>
<form method="post">
{% if form.errors %}
<div class="alert alert-danger" role="alert">
{{ form.errors }}
</div>
{% endif %}
{% csrf_token %} {{ form | crispy }}
<button type="submit" class="btn btn-primary">Save</button>
</form>

View file

@ -3,6 +3,11 @@
{% load crispy_forms_tags %} {% block content %}
<h1>{% if form.instance.pk %}Edit{% else %}Create{% endif %} Prefix</h1>
<form method="post">
{% if form.errors %}
<div class="alert alert-danger" role="alert">
{{ form.errors }}
</div>
{% endif %}
{% csrf_token %} {{ form | crispy }}
<input class="btn btn-primary" type="submit" value="Save" />
</form>

View file

@ -4,6 +4,11 @@
<div class="container mt-5">
<h1>{% if form.instance.pk %}Edit{% else %}Create{% endif %} Suffix</h1>
<form method="post">
{% if form.errors %}
<div class="alert alert-danger" role="alert">
{{ form.errors }}
</div>
{% endif %}
{% csrf_token %} {{ form | crispy }}
<p class="text-muted">Please make sure to include a full description of
your intended use of the suffix in the "Description" field. Your request