diff --git a/app/models/validations/sales/household_validations.rb b/app/models/validations/sales/household_validations.rb index ec73b4abb..666886865 100644 --- a/app/models/validations/sales/household_validations.rb +++ b/app/models/validations/sales/household_validations.rb @@ -6,6 +6,7 @@ module Validations::Sales::HouseholdValidations validate_person_age_matches_relationship(record, n) validate_person_age_and_relationship_matches_economic_status(record, n) validate_person_age_matches_economic_status(record, n) + validate_child_12_years_younger(record, n) end shared_validate_partner_count(record, 6) end @@ -79,6 +80,19 @@ private end end + def validate_child_12_years_younger(record, person_num) + buyer_1_age = record.public_send("age1") + person_age = record.public_send("age#{person_num}") + relationship = record.public_send("relat#{person_num}") + return unless buyer_1_age && person_age && relationship + + if person_age > buyer_1_age - 12 && person_is_child?(relationship) + record.errors.add "age1", I18n.t("validations.household.age.child_12_years_younger") + record.errors.add "age#{person_num}", I18n.t("validations.household.age.child_12_years_younger") + record.errors.add "relat#{person_num}", I18n.t("validations.household.relat.child_12_years_younger") + end + end + def person_is_partner?(relationship) relationship == "P" end diff --git a/config/locales/en.yml b/config/locales/en.yml index 2d17cf207..b66092e30 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -341,6 +341,7 @@ en: child_under_16: "Answer cannot be under 16 as person’s %{person_num} working situation is not ‘child under 16’" child_over_16: "Answer cannot be over 16 as person’s %{person_num} working situation is ‘child under 16‘" child_over_20: "Answer cannot be 20 or over as the relationship is ‘child’" + child_12_years_younger: "A child must be at least 12 years younger than their parent" not_student_16_19: "Answer cannot be between 16 and 19 as person %{person_num} is a child of the lead tenant but is not a full-time student" student_16_19: cannot_be_16_19: