diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 46d766ccc..9a84c633e 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -15,6 +15,16 @@ class CaseLogValidator < ActiveModel::Validator end end + def validate_other_reason_for_leaving_last_settled_home(record) + if record.reason_for_leaving_last_settled_home == "Other" && record.other_reason_for_leaving_last_settled_home.blank? + record.errors.add :other_reason_for_leaving_last_settled_home, "If reason for leaving settled home is other then the other reason must be provided" + end + + if record.reason_for_leaving_last_settled_home != "Other" && record.other_reason_for_leaving_last_settled_home.present? + record.errors.add :other_reason_for_leaving_last_settled_home, "The other reason must not be provided if the reason for leaving settled home was not other" + end + end + def validate(record) # If we've come from the form UI we only want to validate the specific fields # that have just been submitted. If we're submitting a log via API or Bulk Upload @@ -87,6 +97,14 @@ private end def mandatory_fields - attributes.except(*AUTOGENERATED_FIELDS) + required = attributes.except(*AUTOGENERATED_FIELDS) + + dynamically_not_required = [] + + if reason_for_leaving_last_settled_home != "Other" + dynamically_not_required << "other_reason_for_leaving_last_settled_home" + end + + required.delete_if { |key, _value| dynamically_not_required.include?(key) } end end diff --git a/spec/fixtures/complete_case_log.json b/spec/fixtures/complete_case_log.json index 11f073714..da2e0da20 100644 --- a/spec/fixtures/complete_case_log.json +++ b/spec/fixtures/complete_case_log.json @@ -89,7 +89,7 @@ "chr_letting": false, "cap_letting": false, "outstanding_rent_or_charges": 25, - "other_reason_for_leaving_last_settled_home": "Other reason", + "other_reason_for_leaving_last_settled_home": null, "accessibility_requirements_fully_wheelchair_accessible_housing": true, "accessibility_requirements_wheelchair_access_to_essential_rooms": false, "accessibility_requirements_level_access_housing": false, diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 6b0805250..dff400389 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -25,6 +25,22 @@ RSpec.describe Form, type: :model do it "validates number of relets is over 0" do expect { CaseLog.create!(property_number_of_times_relet: 0) }.to raise_error(ActiveRecord::RecordInvalid) end + + context "other reason for leaving last settled home validation" do + it "must be provided if main reason for leaving last settled home was given as other" do + expect { + CaseLog.create!(reason_for_leaving_last_settled_home: "Other", + other_reason_for_leaving_last_settled_home: nil) + }.to raise_error(ActiveRecord::RecordInvalid) + end + + it "must not be provided if the main reason for leaving settled home is not other" do + expect { + CaseLog.create!(reason_for_leaving_last_settled_home: "Repossession", + other_reason_for_leaving_last_settled_home: "the other reason provided") + }.to raise_error(ActiveRecord::RecordInvalid) + end + end end describe "status" do