diff --git a/app/models/case_log.rb b/app/models/case_log.rb index aad4d23b0..f56f20e2f 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -55,6 +55,17 @@ class CaseLogValidator < ActiveModel::Validator end end + def validate_armed_forces_active_response(record) + # binding.pry + if record.armed_forces == "Yes - a regular" && record.armed_forces_active.blank? + record.errors.add :armed_forces_active, "You must answer the armed forces active question if the tenant has served as a regular in the armed forces" + end + + if record.armed_forces != "Yes - a regular" && !record.armed_forces_active.blank? + record.errors.add :armed_forces_active, "You must not answer the armed forces active question if the tenant has not served as a regular in the armed forces" + 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 cc619a041..e53c54957 100644 --- a/spec/helpers/check_answers_helper_spec.rb +++ b/spec/helpers/check_answers_helper_spec.rb @@ -11,7 +11,9 @@ RSpec.describe CheckAnswersHelper do ) end let(:case_log_with_met_radio_condition) do - FactoryBot.create(:case_log, armed_forces: "Yes - a regular", armed_forces_injured: "No") + FactoryBot.create(:case_log, armed_forces: "Yes - a regular", + armed_forces_injured: "No", + armed_forces_active: "Yes") end let(:subsection) { "income_and_benefits" } let(:subsection_with_numeric_conditionals) { "household_characteristics" } @@ -56,7 +58,7 @@ RSpec.describe CheckAnswersHelper do subsection_with_radio_conditionals, case_log_with_met_radio_condition, form, - )).to equal(3) + )).to equal(4) end end diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 265296e40..5fcb41bec 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -96,6 +96,31 @@ RSpec.describe Form, type: :model do }.to raise_error(ActiveRecord::RecordInvalid) end end + + context "armed forces active validation" do + it "must be answered if ever served in the forces as a regular" do + expect { + CaseLog.create!(armed_forces: "Yes - a regular", + armed_forces_active: nil) + }.to raise_error(ActiveRecord::RecordInvalid) + end + + it "must not be answered if not ever served as a regular" do + expect { + CaseLog.create!(armed_forces: "No", + armed_forces_active: "Yes") + }.to raise_error() + end + + #Crossover over tests here as injured must be answered as well for no error + it "must be answered if ever served in the forces as a regular" do + expect { + CaseLog.create!(armed_forces: "Yes - a regular", + armed_forces_active: "Yes", + armed_forces_injured: "Yes") + }.not_to raise_error() + end + end end describe "status" do