From bf34f02544a25bd22a7b7e4831bfc85d2d0498ab Mon Sep 17 00:00:00 2001 From: Caitlin Barnard Date: Mon, 18 Nov 2024 12:17:49 +0000 Subject: [PATCH] Rework validation --- payroll/forms.py | 40 +++---------- ...0_alter_vacancy_appointee_name_and_more.py | 59 +++++++++++++++++++ payroll/models.py | 41 +++++++++++-- .../templates/payroll/page/add_vacancy.html | 31 +++------- .../payroll/partials/_error_summary.html | 26 ++++++++ .../{ => payroll/partials}/_form_field.html | 0 6 files changed, 137 insertions(+), 60 deletions(-) create mode 100644 payroll/migrations/0010_alter_vacancy_appointee_name_and_more.py create mode 100644 payroll/templates/payroll/partials/_error_summary.html rename payroll/templates/{ => payroll/partials}/_form_field.html (100%) diff --git a/payroll/forms.py b/payroll/forms.py index acc72724..d0efbc5a 100644 --- a/payroll/forms.py +++ b/payroll/forms.py @@ -5,29 +5,6 @@ class VacancyForm(forms.ModelForm): - - appointee_name = forms.CharField( - validators=[ - lambda value: validate_only_letters_numbers_spaces(value, "Appointee name") - ], - widget=forms.TextInput(attrs={"class": "govuk-input govuk-input--width-20"}), - required=False, - ) - hiring_manager = forms.CharField( - validators=[ - lambda value: validate_only_letters_numbers_spaces(value, "Hiring manager") - ], - widget=forms.TextInput(attrs={"class": "govuk-input govuk-input--width-20"}), - required=False, - ) - hr_ref = forms.CharField( - validators=[ - lambda value: validate_only_letters_numbers_spaces(value, "HR ref") - ], - widget=forms.TextInput(attrs={"class": "govuk-input govuk-input--width-20"}), - required=False, - ) - class Meta: model = Vacancy fields = "__all__" @@ -37,12 +14,13 @@ class Meta: "grade": forms.Select(attrs={"class": "govuk-select"}), "recruitment_stage": forms.Select(attrs={"class": "govuk-select"}), "programme_code": forms.Select(attrs={"class": "govuk-select"}), - } - error_messages = { - "grade": { - "required": "Grade is a required field.", - }, - "programme_code": { - "required": "Programme code is a required field.", - }, + "appointee_name": forms.TextInput( + attrs={"class": "govuk-input govuk-input--width-20"} + ), + "hiring_manager": forms.TextInput( + attrs={"class": "govuk-input govuk-input--width-20"} + ), + "hr_ref": forms.TextInput( + attrs={"class": "govuk-input govuk-input--width-20"} + ), } diff --git a/payroll/migrations/0010_alter_vacancy_appointee_name_and_more.py b/payroll/migrations/0010_alter_vacancy_appointee_name_and_more.py new file mode 100644 index 00000000..23dd74ba --- /dev/null +++ b/payroll/migrations/0010_alter_vacancy_appointee_name_and_more.py @@ -0,0 +1,59 @@ +# Generated by Django 4.2.16 on 2024-11-18 12:07 + +import django.core.validators +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("payroll", "0009_remove_vacancy_programme_switch_vacancy"), + ] + + operations = [ + migrations.AlterField( + model_name="vacancy", + name="appointee_name", + field=models.CharField( + blank=True, + max_length=255, + null=True, + validators=[ + django.core.validators.RegexValidator( + message="Only letters, spaces, - and ' are allowed", + regex="^[a-zA-Z '-]*$", + ) + ], + ), + ), + migrations.AlterField( + model_name="vacancy", + name="hiring_manager", + field=models.CharField( + blank=True, + max_length=255, + null=True, + validators=[ + django.core.validators.RegexValidator( + message="Only letters, spaces, - and ' are allowed", + regex="^[a-zA-Z '-]*$", + ) + ], + ), + ), + migrations.AlterField( + model_name="vacancy", + name="hr_ref", + field=models.CharField( + blank=True, + max_length=255, + null=True, + validators=[ + django.core.validators.RegexValidator( + message="Only letters, spaces, - and ' are allowed", + regex="^[a-zA-Z '-]*$", + ) + ], + ), + ), + ] diff --git a/payroll/models.py b/payroll/models.py index 9ce1e5e9..a9f998f2 100644 --- a/payroll/models.py +++ b/payroll/models.py @@ -1,4 +1,5 @@ from django.db import models +from django.core.validators import RegexValidator class Employee(models.Model): @@ -142,9 +143,7 @@ class Meta: cost_centre = models.ForeignKey("costcentre.CostCentre", models.PROTECT) grade = models.ForeignKey("gifthospitality.Grade", models.PROTECT) - programme_code = models.ForeignKey( - "chartofaccountDIT.ProgrammeCode", models.PROTECT - ) + programme_code = models.ForeignKey("chartofaccountDIT.ProgrammeCode", models.PROTECT) recruitment_type = models.CharField( max_length=29, choices=RecruitmentType.choices, @@ -154,6 +153,36 @@ class Meta: choices=RecruitmentStage.choices, default=RecruitmentStage.PREPARING ) - appointee_name = models.CharField(max_length=255, null=True, blank=True) - hiring_manager = models.CharField(max_length=255, null=True, blank=True) - hr_ref = models.CharField(max_length=255, null=True, blank=True) + appointee_name = models.CharField( + max_length=255, + null=True, + blank=True, + validators=[ + RegexValidator( + regex=r"^[a-zA-Z '-]*$", + message="Only letters, spaces, - and ' are allowed", + ) + ], + ) + hiring_manager = models.CharField( + max_length=255, + null=True, + blank=True, + validators=[ + RegexValidator( + regex=r"^[a-zA-Z '-]*$", + message="Only letters, spaces, - and ' are allowed", + ) + ], + ) + hr_ref = models.CharField( + max_length=255, + null=True, + blank=True, + validators=[ + RegexValidator( + regex=r"^[a-zA-Z '-]*$", + message="Only letters, spaces, - and ' are allowed", + ) + ], + ) diff --git a/payroll/templates/payroll/page/add_vacancy.html b/payroll/templates/payroll/page/add_vacancy.html index b111db36..e824fa8e 100644 --- a/payroll/templates/payroll/page/add_vacancy.html +++ b/payroll/templates/payroll/page/add_vacancy.html @@ -13,33 +13,18 @@ {% block content %}

Create Vacancy

- {% if form.errors %} - - {% endif %} + {% include "payroll/partials/_error_summary.html" with form=form %}
{% csrf_token %}
- {% include "_form_field.html" with field=form.recruitment_type %} - {% include "_form_field.html" with field=form.grade %} - {% include "_form_field.html" with field=form.recruitment_stage %} - {% include "_form_field.html" with field=form.programme_code %} - {% include "_form_field.html" with field=form.appointee_name %} - {% include "_form_field.html" with field=form.hiring_manager %} - {% include "_form_field.html" with field=form.hr_ref %} + {% include "payroll/partials/_form_field.html" with field=form.recruitment_type %} + {% include "payroll/partials/_form_field.html" with field=form.grade %} + {% include "payroll/partials/_form_field.html" with field=form.recruitment_stage %} + {% include "payroll/partials/_form_field.html" with field=form.programme_code %} + {% include "payroll/partials/_form_field.html" with field=form.appointee_name %} + {% include "payroll/partials/_form_field.html" with field=form.hiring_manager %} + {% include "payroll/partials/_form_field.html" with field=form.hr_ref %}
Back diff --git a/payroll/templates/payroll/partials/_error_summary.html b/payroll/templates/payroll/partials/_error_summary.html new file mode 100644 index 00000000..e0864dfb --- /dev/null +++ b/payroll/templates/payroll/partials/_error_summary.html @@ -0,0 +1,26 @@ +{% if form.errors %} + +{% endif %} \ No newline at end of file diff --git a/payroll/templates/_form_field.html b/payroll/templates/payroll/partials/_form_field.html similarity index 100% rename from payroll/templates/_form_field.html rename to payroll/templates/payroll/partials/_form_field.html