diff --git a/app/models/validations/sales/sale_information_validations.rb b/app/models/validations/sales/sale_information_validations.rb index c42c42ccc..30c9a7584 100644 --- a/app/models/validations/sales/sale_information_validations.rb +++ b/app/models/validations/sales/sale_information_validations.rb @@ -54,4 +54,12 @@ module Validations::Sales::SaleInformationValidations end end end + + def validate_mortgage_length(record) + return unless record.mortlen + + return if record.mortlen >= 0 && record.mortlen <= 60 + + record.errors.add :mortlen, I18n.t("validations.sale_information.mortlen.range") + end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 3dd612e8e..0c53616a4 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -426,6 +426,8 @@ en: previous_property_type: property_type_bedsit: "A bedsit can not have more than 1 bedroom" discounted_ownership_value: "Mortgage, deposit, and grant total must equal £%{value_with_discount}" + mortlen: + range: Mortgage length must be between 0 and 60 soft_validations: net_income: diff --git a/spec/models/validations/sales/sale_information_validations_spec.rb b/spec/models/validations/sales/sale_information_validations_spec.rb index d2921e8f0..29a162337 100644 --- a/spec/models/validations/sales/sale_information_validations_spec.rb +++ b/spec/models/validations/sales/sale_information_validations_spec.rb @@ -145,7 +145,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do describe "#validate_previous_property_unit_type" do context "when number of bedrooms is <= 1" do - let(:record) { FactoryBot.build(:sales_log, frombeds: 1, fromprop: 2) } + let(:record) { build(:sales_log, frombeds: 1, fromprop: 2) } it "does not add an error if it's a bedsit" do sale_information_validator.validate_previous_property_unit_type(record) @@ -155,7 +155,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end context "when number of bedrooms is > 1" do - let(:record) { FactoryBot.build(:sales_log, frombeds: 2, fromprop: 2) } + let(:record) { build(:sales_log, frombeds: 2, fromprop: 2) } it "does add an error if it's a bedsit" do sale_information_validator.validate_previous_property_unit_type(record) @@ -371,4 +371,36 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end end end + + describe "#validate_mortgage_length" do + context "when mortlen is < 0" do + let(:record) { build(:sales_log, mortlen: -1) } + + it "does not add an error if it's a bedsit" do + sale_information_validator.validate_mortgage_length(record) + + expect(record.errors[:mortlen]).to include(I18n.t("validations.sale_information.mortlen.range")) + end + end + + context "when 0<= mortlen <= 60" do + let(:record) { build(:sales_log, mortlen: 20) } + + it "does not add an error if it's a bedsit" do + sale_information_validator.validate_mortgage_length(record) + + expect(record.errors).to be_empty + end + end + + context "when mortlen > 60" do + let(:record) { build(:sales_log, mortlen: 61) } + + it "does not add an error if it's a bedsit" do + sale_information_validator.validate_mortgage_length(record) + + expect(record.errors[:mortlen]).to include(I18n.t("validations.sale_information.mortlen.range")) + end + end + end end