Browse Source

validation init

pull/39/head
Matthew Phelan 3 years ago
parent
commit
7d762bd4f3
  1. 8
      app/controllers/case_logs_controller.rb
  2. 16
      app/models/case_log.rb
  3. 2
      db/schema.rb

8
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)

16
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

2
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"

Loading…
Cancel
Save