diff --git a/app/models/validations/shared_validations.rb b/app/models/validations/shared_validations.rb index d83c1801a..9b4f8a3ff 100644 --- a/app/models/validations/shared_validations.rb +++ b/app/models/validations/shared_validations.rb @@ -1,9 +1,9 @@ module Validations::SharedValidations - def validate_other_field(record, value_other = nil, main_field = nil, other_field = nil) + def validate_other_field(record, value_other = nil, main_field = nil, other_field = nil, main_label = nil, other_label = nil) return unless main_field || other_field - main_field_label = main_field.to_s.humanize(capitalize: false) - other_field_label = other_field.to_s.humanize(capitalize: false) + main_field_label = main_label || main_field.to_s.humanize(capitalize: false) + other_field_label = other_label || other_field.to_s.humanize(capitalize: false) if record[main_field] == value_other && record[other_field].blank? record.errors.add other_field.to_sym, I18n.t("validations.other_field_missing", main_field_label:, other_field_label:) end diff --git a/app/models/validations/tenancy_validations.rb b/app/models/validations/tenancy_validations.rb index 008843053..25e09f205 100644 --- a/app/models/validations/tenancy_validations.rb +++ b/app/models/validations/tenancy_validations.rb @@ -30,7 +30,7 @@ module Validations::TenancyValidations end def validate_other_tenancy_type(record) - validate_other_field(record, 3, :tenancy, :tenancyother) + validate_other_field(record, 3, :tenancy, :tenancyother, "tenancy type", "other tenancy type") end def validate_joint_tenancy(record) diff --git a/spec/models/validations/tenancy_validations_spec.rb b/spec/models/validations/tenancy_validations_spec.rb index 9e80fc2d1..fe2792243 100644 --- a/spec/models/validations/tenancy_validations_spec.rb +++ b/spec/models/validations/tenancy_validations_spec.rb @@ -198,8 +198,9 @@ RSpec.describe Validations::TenancyValidations do describe "tenancy type validations" do let(:field) { "validations.other_field_missing" } - let(:main_field_label) { "tenancy" } - let(:other_field_label) { "tenancyother" } + let(:main_field_label) { "tenancy type" } + let(:other_field) { "tenancyother" } + let(:other_field_label) { "other tenancy type" } let(:expected_error) { I18n.t(field, main_field_label:, other_field_label:) } context "when tenancy type is other" do @@ -208,13 +209,13 @@ RSpec.describe Validations::TenancyValidations do it "validates that other tenancy type is provided" do record.tenancyother = nil tenancy_validator.validate_other_tenancy_type(record) - expect(record.errors[other_field_label]).to include(match(expected_error)) + expect(record.errors[other_field]).to include(match(expected_error)) end it "expects that other tenancy type is provided" do record.tenancyother = "Some other tenancy type" tenancy_validator.validate_other_tenancy_type(record) - expect(record.errors[other_field_label]).to be_empty + expect(record.errors[other_field]).to be_empty end end @@ -225,14 +226,14 @@ RSpec.describe Validations::TenancyValidations do record.tenancy = 2 record.tenancyother = "Some other tenancy type" tenancy_validator.validate_other_tenancy_type(record) - expect(record.errors[other_field_label]).to include(match(expected_error)) + expect(record.errors[other_field]).to include(match(expected_error)) end it "expects that other tenancy type is not provided" do record.tenancy = 1 record.tenancyother = nil tenancy_validator.validate_other_tenancy_type(record) - expect(record.errors[other_field_label]).to be_empty + expect(record.errors[other_field]).to be_empty end end end