Browse Source

Restore invalid postcode input value in postcode field

CLDC-3790-continued
Kat 1 week ago
parent
commit
4bec6cb8aa
  1. 9
      app/controllers/form_controller.rb
  2. 1
      app/models/form/lettings/questions/postcode_for_full_address.rb
  3. 2
      app/models/form/question.rb
  4. 4
      app/models/log.rb
  5. 19
      spec/requests/form_controller_spec.rb

9
app/controllers/form_controller.rb

@ -93,6 +93,7 @@ class FormController < ApplicationController
@questions = request.params["related_question_ids"].map { |id| @log.form.get_question(id, @log) } @questions = request.params["related_question_ids"].map { |id| @log.form.get_question(id, @log) }
render "form/check_errors" render "form/check_errors"
else else
restore_placeholder_values(@log, @page)
if flash[:errors].present? if flash[:errors].present?
restore_previous_errors(flash[:errors]) restore_previous_errors(flash[:errors])
restore_error_field_values(flash[:log_data]) restore_error_field_values(flash[:log_data])
@ -125,6 +126,14 @@ private
@log.assign_attributes(previous_responses_to_reset) @log.assign_attributes(previous_responses_to_reset)
end end
def restore_placeholder_values(log, page)
page.questions.each do |question|
next if question.placeholder_method.blank?
log[question.id] ||= log.send(question.placeholder_method)
end
end
def restore_previous_errors(previous_errors) def restore_previous_errors(previous_errors)
return unless previous_errors return unless previous_errors

1
app/models/form/lettings/questions/postcode_for_full_address.rb

@ -20,6 +20,7 @@ class Form::Lettings::Questions::PostcodeForFullAddress < ::Form::Question
@disable_clearing_if_not_routed_or_dynamic_answer_options = true @disable_clearing_if_not_routed_or_dynamic_answer_options = true
@question_number = QUESTION_NUMBER_FROM_YEAR[form.start_date.year] || QUESTION_NUMBER_FROM_YEAR[QUESTION_NUMBER_FROM_YEAR.keys.max] @question_number = QUESTION_NUMBER_FROM_YEAR[form.start_date.year] || QUESTION_NUMBER_FROM_YEAR[QUESTION_NUMBER_FROM_YEAR.keys.max]
@hide_question_number_on_page = true @hide_question_number_on_page = true
@placeholder_method = "postcode_full_input_placeholder"
end end
QUESTION_NUMBER_FROM_YEAR = { 2023 => 12, 2024 => 13 }.freeze QUESTION_NUMBER_FROM_YEAR = { 2023 => 12, 2024 => 13 }.freeze

2
app/models/form/question.rb

@ -8,7 +8,7 @@ class Form::Question
:top_guidance_partial, :bottom_guidance_partial, :prefix, :suffix, :top_guidance_partial, :bottom_guidance_partial, :prefix, :suffix,
:requires_js, :fields_added, :derived, :check_answers_card_number, :requires_js, :fields_added, :derived, :check_answers_card_number,
:unresolved_hint_text, :question_number, :hide_question_number_on_page, :unresolved_hint_text, :question_number, :hide_question_number_on_page,
:plain_label, :error_label :plain_label, :error_label, :placeholder_method
def initialize(id, hsh, page) def initialize(id, hsh, page)
@id = id @id = id

4
app/models/log.rb

@ -294,6 +294,10 @@ class Log < ApplicationRecord
!!public_send("age#{person_num}_known")&.zero? !!public_send("age#{person_num}_known")&.zero?
end end
def postcode_full_input_placeholder
postcode_full_input
end
private private
# Handle logs that are older than previous collection start date # Handle logs that are older than previous collection start date

19
spec/requests/form_controller_spec.rb

@ -371,8 +371,10 @@ RSpec.describe FormController, type: :request do
end end
describe "GET" do describe "GET" do
let(:current_time) { Time.zone.local(2022, 5, 1) }
around do |example| around do |example|
Timecop.freeze(Time.zone.local(2022, 5, 1)) do Timecop.freeze(current_time) do
Singleton.__init__(FormHandler) Singleton.__init__(FormHandler)
example.run example.run
end end
@ -443,6 +445,21 @@ RSpec.describe FormController, type: :request do
end end
end end
end end
context "when viewing the manual adress entry page" do
let(:current_time) { Time.zone.local(2024, 5, 1) }
context "and postcode input exists but postcode is not set" do
let(:user) { create(:user, :support) }
let(:lettings_log) { create(:lettings_log, :setup_completed, postcode_full_input: "A1", address_line1_input: "xx", postcode_full: nil, uprn_known: 0, uprn_selection: "uprn_not_listed") }
it "displays the postcode_full_input in postcode_full field" do
get "/lettings-logs/#{lettings_log.id}/address", headers: headers, params: {}
expect(page).to have_field("lettings-log-postcode-full-field", with: "A1")
expect(lettings_log.reload.postcode_full).to eq(nil)
end
end
end
end end
context "when displaying check answers pages" do context "when displaying check answers pages" do

Loading…
Cancel
Save