diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 8fad9709c..934ec42a8 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -39,6 +39,16 @@ class CaseLogValidator < ActiveModel::Validator end end + def validate_armed_forces_injured(record) + if (record.armed_forces == "Yes - a regular" || record.armed_forces == "Yes - a reserve") && record.armed_forces_injured.blank? + record.errors.add :armed_forces_injured, "You must answer the armed forces injury question if the tenant has served in the armed forces" + end + + if (record.armed_forces == "No" || record.armed_forces == "Prefer not to say") && record.armed_forces_injured.present? + record.errors.add :armed_forces_injured, "You must not answer the armed forces injury question if the tenant has not served in the armed forces or prefer not to say was chosen" + 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 diff --git a/spec/helpers/check_answers_helper_spec.rb b/spec/helpers/check_answers_helper_spec.rb index 120c7947c..3967bc221 100644 --- a/spec/helpers/check_answers_helper_spec.rb +++ b/spec/helpers/check_answers_helper_spec.rb @@ -11,7 +11,7 @@ RSpec.describe CheckAnswersHelper do ) end let(:case_log_with_met_radio_condition) do - FactoryBot.create(:case_log, armed_forces: "Yes - a regular") + FactoryBot.create(:case_log, armed_forces: "Yes - a regular", armed_forces_injured: "No") end let(:subsection) { "income_and_benefits" } let(:subsection_with_numeric_conditionals) { "household_characteristics" } diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index d3e4d74a9..927b153d1 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -26,7 +26,7 @@ RSpec.describe Form, type: :model do expect { CaseLog.create!(property_number_of_times_relet: 0) }.to raise_error(ActiveRecord::RecordInvalid) end - describe "reasonable preference validation" do + context "reasonable preference validation" do it "if given reasonable preference is yes a reason must be selected" do expect { CaseLog.create!(reasonable_preference: "Yes", @@ -57,6 +57,7 @@ RSpec.describe Form, type: :model do }.to raise_error(ActiveRecord::RecordInvalid) end 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 { @@ -72,6 +73,22 @@ RSpec.describe Form, type: :model do }.to raise_error(ActiveRecord::RecordInvalid) end end + + context "armed forces injured validation" do + it "must be anwered if tenant was a regular or reserve in armed forces" do + expect { + CaseLog.create!(armed_forces: "Yes - a regular", + armed_forces_injured: nil) + }.to raise_error(ActiveRecord::RecordInvalid) + end + + it "must be anwered if tenant was not a regular or reserve in armed forces" do + expect { + CaseLog.create!(armed_forces: "No", + armed_forces_injured: "Yes") + }.to raise_error(ActiveRecord::RecordInvalid) + end + end end describe "status" do