diff --git a/app/models/validations/household_validations.rb b/app/models/validations/household_validations.rb index 083d56d37..c6ab9293e 100644 --- a/app/models/validations/household_validations.rb +++ b/app/models/validations/household_validations.rb @@ -19,6 +19,10 @@ module Validations::HouseholdValidations "Rough sleeping", ].freeze + PHRASES_INDICATING_HOMELESSNESS_REGEX = Regexp.union( + PHRASES_INDICATING_HOMELESSNESS.map { |phrase| Regexp.new("\\A[^[:alpha:]]*#{phrase}[^[:alpha:]]*\\Z", Regexp::IGNORECASE) }, + ) + def validate_reason_for_leaving_last_settled_home(record) if record.reason == 32 && record.underoccupation_benefitcap != 4 record.errors.add :underoccupation_benefitcap, I18n.t("validations.household.underoccupation_benefitcap.dont_know_required") @@ -33,7 +37,7 @@ module Validations::HouseholdValidations return unless record.form.start_year_after_2024? - if record.reason == 20 && Regexp.union(PHRASES_INDICATING_HOMELESSNESS.map { |phrase| Regexp.new("\\A[^[:alpha:]]*#{phrase}[^[:alpha:]]*\\Z", Regexp::IGNORECASE) }).match?(record.reasonother) + if record.reason == 20 && PHRASES_INDICATING_HOMELESSNESS_REGEX.match?(record.reasonother) record.errors.add :reason, I18n.t("validations.household.reason.other_not_settled") end end diff --git a/app/models/validations/soft_validations.rb b/app/models/validations/soft_validations.rb index 022a5f038..74afef27d 100644 --- a/app/models/validations/soft_validations.rb +++ b/app/models/validations/soft_validations.rb @@ -163,8 +163,12 @@ module Validations::SoftValidations "Hospital", ].freeze + PHRASES_LIKELY_TO_INDICATE_EXISTING_REASON_CATEGORY_REGEX = Regexp.union( + PHRASES_LIKELY_TO_INDICATE_EXISTING_REASON_CATEGORY.map { |phrase| Regexp.new("\\b[^[:alpha]]*#{phrase}[^[:alpha:]]*\\b", Regexp::IGNORECASE) }, + ) + def reasonother_might_be_existing_category? - Regexp.union(PHRASES_LIKELY_TO_INDICATE_EXISTING_REASON_CATEGORY.map { |phrase| Regexp.new("\\b[^[:alpha]]*#{phrase}[^[:alpha:]]*\\b", Regexp::IGNORECASE) }).match?(reasonother) + PHRASES_LIKELY_TO_INDICATE_EXISTING_REASON_CATEGORY_REGEX.match?(reasonother) end private