Browse Source

Separate messages depending on field they're added to

pull/2743/head
Manny Dinssa 8 months ago
parent
commit
986885bc44
  1. 35
      app/models/validations/tenancy_validations.rb
  2. 19
      config/locales/validations/lettings/tenancy.en.yml
  3. 11
      spec/models/validations/tenancy_validations_spec.rb

35
app/models/validations/tenancy_validations.rb

@ -10,10 +10,9 @@ module Validations::TenancyValidations
min_tenancy_length = 1 min_tenancy_length = 1
return if record.tenancylength.to_i.between?(min_tenancy_length, 99) return if record.tenancylength.to_i.between?(min_tenancy_length, 99)
message = I18n.t("validations.lettings.tenancy.length.invalid_fixed", min_tenancy_length:) record.errors.add :needstype, I18n.t("validations.lettings.tenancy.needstype.invalid_fixed_tenancylength", min_tenancy_length:)
record.errors.add :needstype, message record.errors.add :tenancylength, :tenancylength_invalid, message: I18n.t("validations.lettings.tenancy.tenancylength.invalid_fixed_tenancylength", min_tenancy_length:)
record.errors.add :tenancylength, :tenancylength_invalid, message: message record.errors.add :tenancy, I18n.t("validations.lettings.tenancy.tenancy.invalid_fixed_tenancylength", min_tenancy_length:)
record.errors.add :tenancy, message
end end
def validate_general_needs_fixed_tenancy_length_affordable_social_rent(record) def validate_general_needs_fixed_tenancy_length_affordable_social_rent(record)
@ -23,11 +22,10 @@ module Validations::TenancyValidations
min_tenancy_length = 2 min_tenancy_length = 2
return if record.tenancylength.to_i.between?(min_tenancy_length, 99) return if record.tenancylength.to_i.between?(min_tenancy_length, 99)
message = I18n.t("validations.lettings.tenancy.length.invalid_fixed", min_tenancy_length:) record.errors.add :needstype, I18n.t("validations.lettings.tenancy.needstype.invalid_fixed_tenancylength", min_tenancy_length:)
record.errors.add :needstype, message record.errors.add :rent_type, I18n.t("validations.lettings.tenancy.rent_type.invalid_fixed_tenancylength", min_tenancy_length:)
record.errors.add :rent_type, message record.errors.add :tenancylength, :tenancylength_invalid, message: I18n.t("validations.lettings.tenancy.tenancylength.invalid_fixed_tenancylength", min_tenancy_length:)
record.errors.add :tenancylength, :tenancylength_invalid, message: message record.errors.add :tenancy, I18n.t("validations.lettings.tenancy.tenancy.invalid_fixed_tenancylength", min_tenancy_length:)
record.errors.add :tenancy, message
end end
def validate_general_needs_fixed_tenancy_length_intermediate_rent(record) def validate_general_needs_fixed_tenancy_length_intermediate_rent(record)
@ -37,11 +35,10 @@ module Validations::TenancyValidations
min_tenancy_length = 1 min_tenancy_length = 1
return if record.tenancylength.to_i.between?(min_tenancy_length, 99) return if record.tenancylength.to_i.between?(min_tenancy_length, 99)
message = I18n.t("validations.lettings.tenancy.length.invalid_fixed", min_tenancy_length:) record.errors.add :needstype, I18n.t("validations.lettings.tenancy.needstype.invalid_fixed_tenancylength", min_tenancy_length:)
record.errors.add :needstype, message record.errors.add :rent_type, I18n.t("validations.lettings.tenancy.rent_type.invalid_fixed_tenancylength", min_tenancy_length:)
record.errors.add :rent_type, message record.errors.add :tenancylength, :tenancylength_invalid, message: I18n.t("validations.lettings.tenancy.tenancylength.invalid_fixed_tenancylength", min_tenancy_length:)
record.errors.add :tenancylength, :tenancylength_invalid, message: message record.errors.add :tenancy, I18n.t("validations.lettings.tenancy.tenancy.invalid_fixed_tenancylength", min_tenancy_length:)
record.errors.add :tenancy, message
end end
def validate_periodic_tenancy_length(record) def validate_periodic_tenancy_length(record)
@ -50,18 +47,16 @@ module Validations::TenancyValidations
min_tenancy_length = 1 min_tenancy_length = 1
return if record.tenancylength.to_i.between?(min_tenancy_length, 99) return if record.tenancylength.to_i.between?(min_tenancy_length, 99)
message = I18n.t("validations.lettings.tenancy.length.invalid_periodic", min_tenancy_length:) record.errors.add :tenancylength, :tenancylength_invalid, message: I18n.t("validations.lettings.tenancy.tenancylength.invalid_periodic_tenancylength", min_tenancy_length:)
record.errors.add :tenancylength, :tenancylength_invalid, message: message record.errors.add :tenancy, I18n.t("validations.lettings.tenancy.tenancy.invalid_periodic_tenancylength", min_tenancy_length:)
record.errors.add :tenancy, message
end end
def validate_tenancy_length_blank_when_not_required(record) def validate_tenancy_length_blank_when_not_required(record)
return if record.tenancylength.blank? return if record.tenancylength.blank?
return if record.tenancy_type_fixed_term? || record.is_periodic_tenancy? return if record.tenancy_type_fixed_term? || record.is_periodic_tenancy?
message = I18n.t("validations.lettings.tenancy.length.fixed_term_not_required") record.errors.add :tenancylength, :tenancylength_invalid, message: I18n.t("validations.lettings.tenancy.tenancylength.fixed_term_not_required")
record.errors.add :tenancylength, :tenancylength_invalid, message: message record.errors.add :tenancy, I18n.t("validations.lettings.tenancy.tenancy.fixed_term_not_required")
record.errors.add :tenancy, message
end end
def validate_other_tenancy_type(record) def validate_other_tenancy_type(record)

19
config/locales/validations/lettings/tenancy.en.yml

@ -2,10 +2,23 @@ en:
validations: validations:
lettings: lettings:
tenancy: tenancy:
length: needstype:
invalid_fixed_tenancylength: "Enter a tenancy length between %{min_tenancy_length} and 99 years for a tenancy of this type."
invalid_periodic_tenancylength: "Enter a tenancy length between %{min_tenancy_length} and 99 years (or don't specify the length) for a tenancy of this type."
tenancylength:
invalid_fixed_tenancylength: "Enter a tenancy length between %{min_tenancy_length} and 99 years for a tenancy of this type."
invalid_periodic_tenancylength: "Enter a tenancy length between %{min_tenancy_length} and 99 years (or don't specify the length) for a tenancy of this type."
fixed_term_not_required: "You must only answer the length of the tenancy if it's fixed-term."
tenancy:
invalid_fixed_tenancylength: "Enter a tenancy length between %{min_tenancy_length} and 99 years for a tenancy of this type."
invalid_periodic_tenancylength: "Enter a tenancy length between %{min_tenancy_length} and 99 years (or don't specify the length) for a tenancy of this type."
fixed_term_not_required: "You must only answer the length of the tenancy if it's fixed-term." fixed_term_not_required: "You must only answer the length of the tenancy if it's fixed-term."
invalid_fixed: "Enter a tenancy length between %{min_tenancy_length} and 99 years for a tenancy of this type."
invalid_periodic: "Enter a tenancy length between %{min_tenancy_length} and 99 years (or don't specify the length) for a tenancy of this type." rent_type:
invalid_fixed_tenancylength: "Enter a tenancy length between %{min_tenancy_length} and 99 years for a tenancy of this type."
invalid_periodic_tenancylength: "Enter a tenancy length between %{min_tenancy_length} and 99 years (or don't specify the length) for a tenancy of this type."
joint: joint:
sole_tenancy: "This cannot be a joint tenancy as you've told us there's only one person in the household." sole_tenancy: "This cannot be a joint tenancy as you've told us there's only one person in the household."

11
spec/models/validations/tenancy_validations_spec.rb

@ -76,12 +76,12 @@ RSpec.describe Validations::TenancyValidations do
{ {
name: "assured shorthold", name: "assured shorthold",
code: 4, code: 4,
expected_error: ->(min_tenancy_length) { I18n.t("validations.lettings.tenancy.length.invalid_fixed", min_tenancy_length:) }, expected_error: ->(min_tenancy_length) { I18n.t("validations.lettings.tenancy.tenancylength.invalid_fixed_tenancylength", min_tenancy_length:) },
}, },
{ {
name: "secure fixed term", name: "secure fixed term",
code: 6, code: 6,
expected_error: ->(min_tenancy_length) { I18n.t("validations.lettings.tenancy.length.invalid_fixed", min_tenancy_length:) }, expected_error: ->(min_tenancy_length) { I18n.t("validations.lettings.tenancy.tenancylength.invalid_fixed_tenancylength", min_tenancy_length:) },
}, },
] ]
@ -211,7 +211,7 @@ RSpec.describe Validations::TenancyValidations do
periodic_tenancy_case = { periodic_tenancy_case = {
name: "periodic", name: "periodic",
code: 8, code: 8,
expected_error: ->(min_tenancy_length) { I18n.t("validations.lettings.tenancy.length.invalid_periodic", min_tenancy_length:) }, expected_error: ->(min_tenancy_length) { I18n.t("validations.lettings.tenancy.tenancylength.invalid_periodic_tenancylength", min_tenancy_length:) },
} }
error_fields = %w[tenancylength tenancy] error_fields = %w[tenancylength tenancy]
include_examples "adds expected errors based on the tenancy length", periodic_tenancy_case, error_fields, 1 include_examples "adds expected errors based on the tenancy length", periodic_tenancy_case, error_fields, 1
@ -237,9 +237,8 @@ RSpec.describe Validations::TenancyValidations do
it "adds errors to tenancylength and tenancy" do it "adds errors to tenancylength and tenancy" do
tenancy_validator.validate_tenancy_length_blank_when_not_required(record) tenancy_validator.validate_tenancy_length_blank_when_not_required(record)
expected_error = I18n.t("validations.lettings.tenancy.length.fixed_term_not_required") expect(record.errors["tenancylength"]).to include(I18n.t("validations.lettings.tenancy.tenancylength.fixed_term_not_required"))
expect(record.errors["tenancylength"]).to include(expected_error) expect(record.errors["tenancy"]).to include(I18n.t("validations.lettings.tenancy.tenancy.fixed_term_not_required"))
expect(record.errors["tenancy"]).to include(expected_error)
end end
end end

Loading…
Cancel
Save