From 00569330486cbf3567c58c87f89ab2f8ceece3ea Mon Sep 17 00:00:00 2001 From: Matthew Phelan Date: Thu, 7 Oct 2021 12:52:27 +0100 Subject: [PATCH] regex added to form json --- app/controllers/case_logs_controller.rb | 2 +- app/models/case_log.rb | 9 +++++---- config/forms/2021_2022.json | 3 ++- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/app/controllers/case_logs_controller.rb b/app/controllers/case_logs_controller.rb index a14adf2e9..45973a8ba 100644 --- a/app/controllers/case_logs_controller.rb +++ b/app/controllers/case_logs_controller.rb @@ -26,7 +26,7 @@ class CaseLogsController < ApplicationController previous_page = params[:case_log][: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.custom_validator_options = { previous_page: previous_page } + @case_log.custom_validator_options = { previous_page: previous_page, validation: form.questions_for_page(previous_page)[previous_page]["regex_validation"] } if @case_log.update(answers_for_page) redirect_path = form.next_page_redirect_path(previous_page) redirect_to(send(redirect_path, @case_log)) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 4933c7483..cbe687701 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -1,22 +1,23 @@ class CaseLogValidator < ActiveModel::Validator - def validate_tenant_age(record) + def validate_tenant_age(record, validation_regex) + regexp = Regexp.new validation_regex if !record.tenant_age? record.errors.add :base, "Tenant age can't be blank" - elsif !(record.tenant_age.to_s =~ /^[1-9][0-9]?$|^100$/) + elsif !(record.tenant_age.to_s =~ regexp) record.errors.add :base, "Tenant age must be between 0 and 100" end end def validate(record) question_to_validate = options[:previous_page] - + validation_regex = options[:validation] if question_to_validate == "tenant_code" if !record.tenant_code? record.errors.add :base, "Tenant code can't be blank" end elsif question_to_validate == "tenant_age" - validate_tenant_age(record) + validate_tenant_age(record, validation_regex) end end end diff --git a/config/forms/2021_2022.json b/config/forms/2021_2022.json index a5b3072ce..ef311aa6b 100644 --- a/config/forms/2021_2022.json +++ b/config/forms/2021_2022.json @@ -32,7 +32,8 @@ "type": "numeric", "min": 0, "max": 150, - "step": 1 + "step": 1, + "regex_validation" : "^[1-9][0-9]?$|^100$" } } },