Skip to content

Commit

Permalink
Ensure date of birth is suitable
Browse files Browse the repository at this point in the history
A business requirement is to only allow applicants between 21 and 80
years old, as per the previous IRP application.
  • Loading branch information
rjlynch committed Jul 3, 2024
1 parent e15aa52 commit a178b38
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Journeys
module GetATeacherRelocationPayment
class PersonalDetailsForm < PersonalDetailsForm
with_options if: -> { date_of_birth && date_of_birth.is_a?(Date) } do
validates :date_of_birth,
comparison: {
less_than_or_equal_to: ->(_) { 21.years.ago },
message: PersonalDetailsForm.i18n_error_message(:below_min_age)
}

validates :date_of_birth,
comparison: {
greater_than: ->(_) { 80.years.ago },
message: PersonalDetailsForm.i18n_error_message(:over_max_age)
}
end
end
end
end
3 changes: 2 additions & 1 deletion app/models/journeys/get_a_teacher_relocation_payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ module GetATeacherRelocationPayment
"entry-date" => EntryDateForm,
"nationality" => NationalityForm,
"passport-number" => PassportNumberForm,
"headteacher-details" => HeadteacherDetailsForm
"headteacher-details" => HeadteacherDetailsForm,
"personal-details" => PersonalDetailsForm
}
}
end
Expand Down
4 changes: 4 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,10 @@ en:
school_headteacher_name: "Enter the name of the headteacher of the school where you are employed as a teacher"
errors:
school_headteacher_name: "Enter the headteacher's name"
personal_details:
errors:
over_max_age: Age must be below 80
below_min_age: Age must be above 21

check_your_answers:
part_one:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require "rails_helper"

RSpec.describe Journeys::GetATeacherRelocationPayment::PersonalDetailsForm, type: :model do
let(:journey_session) { build(:get_a_teacher_relocation_payment_session) }

subject(:form) do
described_class.new(
journey_session: journey_session,
journey: Journeys::GetATeacherRelocationPayment,
params: ActionController::Parameters.new({})
)
end

describe "validations" do
before do
form.assign_attributes(
day: date_of_birth.day,
month: date_of_birth.month,
year: date_of_birth.year
)

form.valid?
end

context "with too young of a birth date" do
let(:date_of_birth) { 20.years.ago }

it "does not permit the date of birth" do
expect(form.errors[:date_of_birth]).to include("Age must be above 21")
end
end

context "with too old of a birth date" do
let(:date_of_birth) { 80.years.ago }

it "does not permit the date of birth" do
expect(form.errors[:date_of_birth]).to include("Age must be below 80")
end
end

context "with a valid birth date" do
let(:date_of_birth) { 30.years.ago }

it "permits the date of birth" do
expect(form.errors[:date_of_birth]).to be_empty
end
end
end
end

0 comments on commit a178b38

Please sign in to comment.