diff --git a/app/controllers/case_logs_controller.rb b/app/controllers/case_logs_controller.rb index b3bffb4f4..26d84dd28 100644 --- a/app/controllers/case_logs_controller.rb +++ b/app/controllers/case_logs_controller.rb @@ -109,9 +109,13 @@ private day = params["case_log"]["#{question_key}(3i)"] month = params["case_log"]["#{question_key}(2i)"] year = params["case_log"]["#{question_key}(1i)"] - next unless day.present? && month.present? && year.present? + next unless [day, month, year].any?(&:present?) - result[question_key] = Date.new(year.to_i, month.to_i, day.to_i) + result[question_key] = if day.to_i.between?(1, 31) && month.to_i.between?(1, 12) && year.to_i.between?(2000, 2200) + Date.new(year.to_i, month.to_i, day.to_i) + else + Date.new(0, 1, 1) + end end next unless question_params diff --git a/app/models/bulk_upload.rb b/app/models/bulk_upload.rb index f546ea56d..dcab1753f 100644 --- a/app/models/bulk_upload.rb +++ b/app/models/bulk_upload.rb @@ -26,12 +26,14 @@ class BulkUpload else data_range = FIRST_DATA_ROW..last_row data_range.map do |row_num| - case_log = CaseLog.create + case_log = CaseLog.create! map_row(sheet.row(row_num)).each do |attr_key, attr_val| - begin - case_log.update_attribute(attr_key, attr_val) - rescue ArgumentError + update = case_log.update(attr_key => attr_val) + unless update + # TODO: determine what to do when a bulk upload contains field values that don't pass validations end + rescue ArgumentError + # TODO: determine what we want to do when bulk upload contains totally invalid data for a field. end end end diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 5b5dc6f40..2134a766a 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -5,6 +5,7 @@ class CaseLogValidator < ActiveModel::Validator include PropertyValidations include FinancialValidations include TenancyValidations + include DateValidations def validate(record) # If we've come from the form UI we only want to validate the specific fields diff --git a/app/validations/date_validations.rb b/app/validations/date_validations.rb new file mode 100644 index 000000000..b44cb8d53 --- /dev/null +++ b/app/validations/date_validations.rb @@ -0,0 +1,21 @@ +module DateValidations + def validate_property_major_repairs(record) + date_valid?("mrcdate", record) + end + + def validate_tenancy_start_date(record) + date_valid?("startdate", record) + end + + def validate_sale_completion_date(record) + date_valid?("sale_completion_date", record) + end + +private + + def date_valid?(question, record) + if record[question].is_a?(ActiveSupport::TimeWithZone) && record[question].year.zero? + record.errors.add question, "Please enter a valid date" + end + end +end diff --git a/app/views/case_logs/edit.html.erb b/app/views/case_logs/edit.html.erb index 25668b2ac..94fabb9a9 100644 --- a/app/views/case_logs/edit.html.erb +++ b/app/views/case_logs/edit.html.erb @@ -1,6 +1,6 @@ <%= turbo_frame_tag "case_log_form", target: "_top" do %>
-
+

Tasklist for log <%= @case_log.id %>

diff --git a/app/views/case_logs/index.html.erb b/app/views/case_logs/index.html.erb index 7352c304c..ea3d45436 100644 --- a/app/views/case_logs/index.html.erb +++ b/app/views/case_logs/index.html.erb @@ -2,7 +2,7 @@

Your logs

-
+
<%= link_to "Create new log", case_logs_path, method: :post, class: "govuk-button" %> diff --git a/app/views/form/check_answers.html.erb b/app/views/form/check_answers.html.erb index 26b153f8f..46cb5a4f9 100644 --- a/app/views/form/check_answers.html.erb +++ b/app/views/form/check_answers.html.erb @@ -1,6 +1,6 @@ <%= turbo_frame_tag "case_log_form", target: "_top" do %>
-
+

Check the answers you gave for <%= subsection.humanize(capitalize: false) %>

<%= display_answered_questions_summary(subsection, @case_log, form) %> <% form.pages_for_subsection(subsection).each do |page, page_info| %> diff --git a/app/views/form/page.html.erb b/app/views/form/page.html.erb index 238f11439..f29011b99 100644 --- a/app/views/form/page.html.erb +++ b/app/views/form/page.html.erb @@ -4,7 +4,7 @@ <%= turbo_frame_tag "case_log_form", target: "_top" do %>
-
+
<% if page_info["header"].present? %>

<%= page_info["header"] %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 497489b22..34e97e8b9 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -35,25 +35,12 @@ Skip to main content