diff --git a/app/controllers/case_logs_controller.rb b/app/controllers/case_logs_controller.rb index 56919deda..de7501375 100644 --- a/app/controllers/case_logs_controller.rb +++ b/app/controllers/case_logs_controller.rb @@ -27,9 +27,11 @@ class CaseLogsController < ApplicationController previous_page = params[:previous_page] questions_for_page = form.questions_for_page(previous_page).keys answers_for_page = page_params(questions_for_page).select { |k, _v| questions_for_page.include?(k) } - @case_log.update!(answers_for_page) - next_page = form.next_page(previous_page) - redirect_to(send("case_log_#{next_page}_path", @case_log)) + if @case_log.valid? + @case_log.update!(answers_for_page) + next_page = form.next_page(previous_page) + redirect_to(send("case_log_#{next_page}_path", @case_log)) + end end form = Form.new(2021, 2022) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 3f860cc08..01092a240 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -1,3 +1,19 @@ +class CaseLogValidator < ActiveModel::Validator + def validate_tenant_age(record) + if record.tenant_age < 0 + record.errors.add :base, "Age needs to be above 0" + elsif record.tenant_age > 120 + record.errors.add :base, "Age needs to be below 120" + end + binding.pry + end + + def validate(record) + validate_tenant_age(record) + end +end + class CaseLog < ApplicationRecord enum status: { "in progress" => 0, "submitted" => 1 } + validates_with CaseLogValidator end diff --git a/db/schema.rb b/db/schema.rb index e3215a238..4dab74fee 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -27,7 +27,6 @@ ActiveRecord::Schema.define(version: 2021_09_24_143031) do t.string "previous_housing_situation" t.integer "prior_homelessness" t.string "armed_forces" - t.string "postcode" t.string "tenant_economic_status" t.integer "household_number_of_other_members" t.string "person_2_relationship" @@ -58,6 +57,7 @@ ActiveRecord::Schema.define(version: 2021_09_24_143031) do t.integer "person_8_age" t.string "person_8_gender" t.string "person_8_economic" + t.string "postcode" t.string "homelessness" t.string "last_settled_home" t.string "benefit_cap_spare_room_subsidy"