diff --git a/app/forms/journeys/additional_payments_for_teaching/eligible_itt_subject_form.rb b/app/forms/journeys/additional_payments_for_teaching/eligible_itt_subject_form.rb index c609d87215..1904af7ab6 100644 --- a/app/forms/journeys/additional_payments_for_teaching/eligible_itt_subject_form.rb +++ b/app/forms/journeys/additional_payments_for_teaching/eligible_itt_subject_form.rb @@ -5,7 +5,30 @@ class EligibleIttSubjectForm < Form attribute :eligible_itt_subject, :string - validates :eligible_itt_subject, inclusion: {in: :available_options, message: i18n_error_message(:inclusion)} + validates :eligible_itt_subject, + inclusion: { + in: :available_options, + message: i18n_error_message(:inclusion) + }, + unless: :boolean_answer? + + validates :eligible_itt_subject, + inclusion: { + in: :available_options, + message: ->(object, _data) { + "Select yes if you did your #{qualification_to_substring(object.answers.qualification.to_sym)} in #{object.available_subjects.first}" + } + }, + if: :boolean_answer? + + def self.qualification_to_substring(qualification_symbol) + { + undergraduate_itt: "undergraduate initial teacher training (ITT)", + postgraduate_itt: "postgraduate initial teacher training (ITT)", + assessment_only: "assessment", + overseas_recognition: "teaching qualification" + }[qualification_symbol] + end def available_subjects @available_subjects ||= subject_symbols.map(&:to_s) @@ -52,6 +75,10 @@ def save def eligible_itt_subject_changed? journey_session.answers.eligible_itt_subject != eligible_itt_subject end + + def boolean_answer? + !available_subjects.many? + end end end end diff --git a/spec/forms/journeys/additional_payments_for_teaching/eligible_itt_subject_form_spec.rb b/spec/forms/journeys/additional_payments_for_teaching/eligible_itt_subject_form_spec.rb index 18a4a2068f..01ae9565a3 100644 --- a/spec/forms/journeys/additional_payments_for_teaching/eligible_itt_subject_form_spec.rb +++ b/spec/forms/journeys/additional_payments_for_teaching/eligible_itt_subject_form_spec.rb @@ -31,7 +31,7 @@ let(:itt_academic_year) { AcademicYear.new(2020) } - let(:form) do + subject(:form) do described_class.new( journey: journey, journey_session: journey_session, @@ -40,8 +40,6 @@ end describe "validations" do - subject { form } - let(:params) { ActionController::Parameters.new } it do @@ -49,6 +47,30 @@ .in_array(form.available_options) .with_message("Select a subject") end + + context "when single subject available" do + before do + allow(JourneySubjectEligibilityChecker).to receive(:selectable_subject_symbols).and_return([:mathematics]) + end + + let(:answers) do + build( + :additional_payments_answers, + attributes_for( + :additional_payments_answers, + :with_qualification, + itt_academic_year: itt_academic_year, + current_school_id: create(:school, :early_career_payments_eligible).id + ) + ) + end + + it "returns contextual error message" do + expect(subject).to validate_inclusion_of(:eligible_itt_subject) + .in_array(["mathematics"]) + .with_message("Select yes if you did your postgraduate initial teacher training (ITT) in mathematics") + end + end end describe "#available_subjects" do