Skip to content

Commit

Permalink
Rework validation
Browse files Browse the repository at this point in the history
  • Loading branch information
CaitBarnard committed Nov 18, 2024
1 parent e3522a0 commit bf34f02
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 60 deletions.
40 changes: 9 additions & 31 deletions payroll/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__"
Expand 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"}
),
}
59 changes: 59 additions & 0 deletions payroll/migrations/0010_alter_vacancy_appointee_name_and_more.py
Original file line number Diff line number Diff line change
@@ -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 '-]*$",
)
],
),
),
]
41 changes: 35 additions & 6 deletions payroll/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.db import models
from django.core.validators import RegexValidator


class Employee(models.Model):
Expand Down Expand Up @@ -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,
Expand All @@ -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",
)
],
)
31 changes: 8 additions & 23 deletions payroll/templates/payroll/page/add_vacancy.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,18 @@
{% block content %}
<h1 class="govuk-heading-l">Create Vacancy</h1>

{% if form.errors %}
<div class="govuk-error-summary" aria-labelledby="error-summary-title" role="alert" tabindex="-1" data-module="govuk-error-summary">
<h2 class="govuk-error-summary__title" id="error-summary-title">
There is a problem
</h2>
<div class="govuk-error-summary__body">
<ul class="govuk-list govuk-error-summary__list">
{% for field, errors in form.errors.items %}
<li>
<a href="#id_{{ field }}">{{ errors|join:", " }}</a>
</li>
{% endfor %}
</ul>
</div>
</div>
{% endif %}
{% include "payroll/partials/_error_summary.html" with form=form %}

<form method="post" novalidate>
{% csrf_token %}
<div class="govuk-form-group">
{% 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 %}
</div>
<button class="govuk-button" type="submit">Create Vacancy</button>
<a class="govuk-link" href="{% url 'payroll:edit' cost_centre_code financial_year %}">Back</a>
Expand Down
26 changes: 26 additions & 0 deletions payroll/templates/payroll/partials/_error_summary.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{% if form.errors %}
<div class="govuk-error-summary" aria-labelledby="error-summary-title" role="alert" tabindex="-1" data-module="govuk-error-summary">
<h2 class="govuk-error-summary__title" id="error-summary-title">
There is a problem
</h2>
<div class="govuk-error-summary__body">
<ul class="govuk-list govuk-error-summary__list">
{% for field in form %}
{% if field.errors %}
{% for error in field.errors %}
<li>
<a href="#{{ field.id_for_label }}">{{ field.label }} - {{ error }}</a>
</li>
{% endfor %}
{% endif %}
{% endfor %}

{% for error in form.non_field_errors %}
<li>
<a href="#">{{ error }}</a>
</li>
{% endfor %}
</ul>
</div>
</div>
{% endif %}
File renamed without changes.

0 comments on commit bf34f02

Please sign in to comment.