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$" } } },