From 20d399f1dbdda237bd706c123abe57b3f8d4a66a Mon Sep 17 00:00:00 2001 From: Rachael Booth Date: Fri, 19 Jan 2024 17:57:53 +0000 Subject: [PATCH] Only apply hard validation errors to ecstat/age for tenants who exist --- app/models/validations/financial_validations.rb | 14 +++++--------- .../validations/financial_validations_spec.rb | 7 +++++++ 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/app/models/validations/financial_validations.rb b/app/models/validations/financial_validations.rb index 5284cca1b..a1d799624 100644 --- a/app/models/validations/financial_validations.rb +++ b/app/models/validations/financial_validations.rb @@ -38,15 +38,12 @@ module Validations::FinancialValidations :over_hard_max, message: I18n.t("validations.financial.hhmemb.earnings.over_hard_max", earnings: format_as_currency(record.earnings), frequency:), ) - ecstat_fields = %i[ecstat1 ecstat2 ecstat3 ecstat4 ecstat5 ecstat6 ecstat7 ecstat8] - ecstat_fields.each do |field| + (1..record.hhmemb).each do |n| record.errors.add( - field, + "ecstat#{n}", :over_hard_max, message: I18n.t("validations.financial.ecstat.over_hard_max", earnings: format_as_currency(record.earnings), frequency:), ) - end - (2..8).each do |n| next unless record["ecstat#{n}"] == 9 record.errors.add( @@ -70,15 +67,14 @@ module Validations::FinancialValidations :under_hard_min, message: I18n.t("validations.financial.hhmemb.earnings.under_hard_min", earnings: format_as_currency(record.earnings), frequency:), ) - ecstat_fields = %i[ecstat1 ecstat2 ecstat3 ecstat4 ecstat5 ecstat6 ecstat7 ecstat8] - ecstat_fields.each do |field| + (1..record.hhmemb).each do |n| record.errors.add( - field, + "ecstat#{n}", :under_hard_min, message: I18n.t("validations.financial.ecstat.under_hard_min", earnings: format_as_currency(record.earnings), frequency:), ) + # N.B. It is not possible for a change to an age field to increase the hard min end - # N.B. It is not possible for a change to an age field to increase the hard min end end diff --git a/spec/models/validations/financial_validations_spec.rb b/spec/models/validations/financial_validations_spec.rb index 3b3d70546..a93705bd8 100644 --- a/spec/models/validations/financial_validations_spec.rb +++ b/spec/models/validations/financial_validations_spec.rb @@ -270,6 +270,10 @@ RSpec.describe Validations::FinancialValidations do expect(record.errors["age2"]).to be_empty expect(record.errors["age3"]) .to eq(["The household's income of £5,000.00 weekly is too high for the number of adults. Change either the household income or the age of the tenants."]) + (record.hhmemb+1..8).each do |n| + expect(record.errors["ecstat#{n}"]).to be_empty + expect(record.errors["age#{n}"]).to be_empty + end end it "adds errors to relevant fields for each tenant when income is too low" do @@ -285,6 +289,9 @@ RSpec.describe Validations::FinancialValidations do expect(record.errors["ecstat#{n}"]) .to eq(["The household's income of £50.00 weekly is too low given the household’s working situation"]) end + ((record.hhmemb+1)..8).each do |n| + expect(record.errors["ecstat#{n}"]).to be_empty + end end end end