diff --git a/app/forms/journeys/get_a_teacher_relocation_payment/personal_details_form.rb b/app/forms/journeys/get_a_teacher_relocation_payment/personal_details_form.rb new file mode 100644 index 0000000000..74f98fc406 --- /dev/null +++ b/app/forms/journeys/get_a_teacher_relocation_payment/personal_details_form.rb @@ -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 diff --git a/app/models/journeys/get_a_teacher_relocation_payment.rb b/app/models/journeys/get_a_teacher_relocation_payment.rb index 37e9b48739..7e1ceb5c02 100644 --- a/app/models/journeys/get_a_teacher_relocation_payment.rb +++ b/app/models/journeys/get_a_teacher_relocation_payment.rb @@ -18,7 +18,8 @@ module GetATeacherRelocationPayment "entry-date" => EntryDateForm, "nationality" => NationalityForm, "passport-number" => PassportNumberForm, - "headteacher-details" => HeadteacherDetailsForm + "headteacher-details" => HeadteacherDetailsForm, + "personal-details" => PersonalDetailsForm } } end diff --git a/config/locales/en.yml b/config/locales/en.yml index 5a793f3aef..9a7fa4c8c3 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -768,6 +768,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: diff --git a/spec/forms/journeys/get_a_teacher_relocation_payment/personal_details_form_spec.rb b/spec/forms/journeys/get_a_teacher_relocation_payment/personal_details_form_spec.rb new file mode 100644 index 0000000000..09f2a425ef --- /dev/null +++ b/spec/forms/journeys/get_a_teacher_relocation_payment/personal_details_form_spec.rb @@ -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