diff --git a/app/models/validations/household_validations.rb b/app/models/validations/household_validations.rb index 56982f65b..d361e15a1 100644 --- a/app/models/validations/household_validations.rb +++ b/app/models/validations/household_validations.rb @@ -63,11 +63,7 @@ module Validations::HouseholdValidations end def validate_person_1_age(record) - return unless record.age1 - - if !record.age1.is_a?(Integer) || record.age1 < 16 || record.age1 > 120 - record.errors.add :age1, I18n.t("validations.household.age.over_16") - end + validate_person_age(record, 1, 16) end def validate_person_1_economic(record) @@ -110,12 +106,18 @@ private end end - def validate_person_age(record, person_num) + def validate_person_age(record, person_num, lower_bound = 1) age = record.public_send("age#{person_num}") return unless age - if !age.is_a?(Integer) || age < 1 || age > 120 - record.errors.add "age#{person_num}".to_sym, I18n.t("validations.household.age.must_be_valid") + begin + Integer(record.public_send("age#{person_num}_before_type_cast")) + rescue ArgumentError + record.errors.add "age#{person_num}".to_sym, I18n.t("validations.household.age.must_be_valid", lower_bound: lower_bound) + end + + if age < lower_bound || age > 120 + record.errors.add "age#{person_num}".to_sym, I18n.t("validations.household.age.must_be_valid", lower_bound: lower_bound) end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 9163aaefd..6047440be 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -86,8 +86,7 @@ en: preg_occ: no_female: "You must answer no as there are no female tenants aged 16-50 in the property" age: - over_16: "Tenant age must be an integer between 16 and 120" - must_be_valid: "Tenant age must be an integer between 0 and 120" + must_be_valid: "Tenant age must be an integer between %{lower_bound} and 120" retired_male: "Male tenant who is retired must be 65 or over" retired_female: "Female tenant who is retired must be 60 or over" ecstat: