diff --git a/app/models/validations/tenancy_validations.rb b/app/models/validations/tenancy_validations.rb index b03d4844b..01281a931 100644 --- a/app/models/validations/tenancy_validations.rb +++ b/app/models/validations/tenancy_validations.rb @@ -6,11 +6,7 @@ module Validations::TenancyValidations def validate_fixed_term_tenancy(record) is_present = record.tenancylength.present? is_in_range = record.tenancylength.to_i.between?(min_tenancy_length(record), 99) - conditions = [ - { - condition: !(record.is_secure_tenancy? || record.is_assured_shorthold_tenancy?) && is_present, - error: I18n.t("validations.tenancy.length.fixed_term_not_required"), - }, + rent_type_dependent_conditions = [ { condition: (record.is_assured_shorthold_tenancy? && !is_in_range) && is_present, error: I18n.t( @@ -19,18 +15,26 @@ module Validations::TenancyValidations ), }, { - condition: record.is_secure_tenancy? && (!is_in_range && is_present), + condition: (record.is_secure_tenancy? && !is_in_range) && is_present, error: I18n.t( "validations.tenancy.length.secure", min_tenancy_length: min_tenancy_length(record), ), }, ] + rent_type_independent_conditions = [ + { + condition: !(record.is_secure_tenancy? || record.is_assured_shorthold_tenancy?) && is_present, + error: I18n.t("validations.tenancy.length.fixed_term_not_required"), + }, + ] + conditions = rent_type_dependent_conditions + rent_type_independent_conditions conditions.each do |condition| next unless condition[:condition] record.errors.add :needstype, condition[:error] + record.errors.add :rent_type, condition[:error] if rent_type_dependent_conditions.include?(condition) record.errors.add :tenancylength, :tenancylength_invalid, message: condition[:error] record.errors.add :tenancy, condition[:error] end diff --git a/spec/models/validations/tenancy_validations_spec.rb b/spec/models/validations/tenancy_validations_spec.rb index f52c19bb9..ca0e84334 100644 --- a/spec/models/validations/tenancy_validations_spec.rb +++ b/spec/models/validations/tenancy_validations_spec.rb @@ -24,6 +24,7 @@ RSpec.describe Validations::TenancyValidations do record.tenancylength = 10 tenancy_validator.validate_fixed_term_tenancy(record) expect(record.errors["needstype"]).to include(match(expected_error)) + expect(record.errors["rent_type"]).not_to include(match(expected_error)) expect(record.errors["tenancylength"]).to include(match(expected_error)) expect(record.errors["tenancy"]).to include(match(expected_error)) end @@ -44,6 +45,7 @@ RSpec.describe Validations::TenancyValidations do record.tenancylength = 1 tenancy_validator.validate_fixed_term_tenancy(record) expect(record.errors["needstype"]).to include(match(expected_error)) + expect(record.errors["rent_type"]).to include(match(expected_error)) expect(record.errors["tenancylength"]).to include(match(expected_error)) expect(record.errors["tenancy"]).to include(match(expected_error)) end @@ -54,6 +56,7 @@ RSpec.describe Validations::TenancyValidations do record.tenancylength = 100 tenancy_validator.validate_fixed_term_tenancy(record) expect(record.errors["needstype"]).to include(match(expected_error)) + expect(record.errors["rent_type"]).to include(match(expected_error)) expect(record.errors["tenancylength"]).to include(match(expected_error)) expect(record.errors["tenancy"]).to include(match(expected_error)) end