diff --git a/app/helpers/conditional_questions_helper.rb b/app/helpers/conditional_questions_helper.rb index 5a1aa394d..2bed19f38 100644 --- a/app/helpers/conditional_questions_helper.rb +++ b/app/helpers/conditional_questions_helper.rb @@ -3,6 +3,13 @@ module ConditionalQuestionsHelper page.questions.map(&:conditional_for).compact.map(&:keys).flatten end + def find_conditional_question(page, conditional_for, answer_value) + return if conditional_for.nil? + + conditional_key = conditional_for.find { |_, conditional_value| conditional_value.include? answer_value }&.first + page.questions.find { |q| q.id == conditional_key } + end + def display_question_key_div(page, question) "style='display:none;'".html_safe if conditional_questions_for_page(page).include?(question.id) || question.requires_js end diff --git a/app/models/form/page.rb b/app/models/form/page.rb index 2fe6c00d0..333810d5a 100644 --- a/app/models/form/page.rb +++ b/app/models/form/page.rb @@ -27,6 +27,15 @@ class Form::Page subsection.enabled?(case_log) && depends_on_met(case_log) end + # We expect to render only one radio question (with conditionals) + def questions_to_render + if questions.first.type == "radio" + [questions.first] + else + questions + end + end + private def depends_on_met(case_log) diff --git a/app/views/form/_radio_question.html.erb b/app/views/form/_radio_question.html.erb index a079fc21c..dda8f010e 100644 --- a/app/views/form/_radio_question.html.erb +++ b/app/views/form/_radio_question.html.erb @@ -9,10 +9,11 @@ <% if key.starts_with?("divider") %> <%= f.govuk_radio_divider %> <% else %> - <%= f.govuk_radio_button question.id, val, label: { text: val }, **stimulus_html_attributes(question) do %> - <% conditional_key = question.conditional_for&.find { |_, cval| cval.include? val }&.first %> - <% if conditional_key %> - <% conditional_question = @page.questions.find { |q| q.id == conditional_key } %> + <% conditional_question = find_conditional_question(@page, question.conditional_for, val) %> + <% if conditional_question.nil? %> + <%= f.govuk_radio_button question.id, val, label: { text: val }, **stimulus_html_attributes(question) %> + <% else %> + <%= f.govuk_radio_button question.id, val, label: { text: val }, **stimulus_html_attributes(question) do %> <%= render partial: "#{conditional_question.type}_question", locals: { question: conditional_question, caption: caption, page_header: page_header, f: f } %> <% end %> <% end %> diff --git a/app/views/form/page.html.erb b/app/views/form/page.html.erb index d50705ab1..4b0787bef 100644 --- a/app/views/form/page.html.erb +++ b/app/views/form/page.html.erb @@ -29,8 +29,8 @@ <%= form_with model: @case_log, url: form_case_log_path(@case_log), method: "post" do |f| %> <%= f.govuk_error_summary %> - <% @page.questions.map do |question| %> -