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:
parent
da929654c0
commit
6b78601b3c
4 changed files with 33 additions and 0 deletions
|
@ -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:
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue