Browse Source

Only apply hard validation errors to ecstat/age for tenants who exist

pull/2136/head
Rachael Booth 2 years ago
parent
commit
20d399f1db
  1. 14
      app/models/validations/financial_validations.rb
  2. 7
      spec/models/validations/financial_validations_spec.rb

14
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,17 +67,16 @@ 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:),
)
end
# N.B. It is not possible for a change to an age field to increase the hard min
end
end
end
if record.earnings.present? && record.incfreq.blank?
record.errors.add :incfreq, I18n.t("validations.financial.earnings.freq_missing")

7
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

Loading…
Cancel
Save