Browse Source

regex added to form json

pull/39/head
Matthew Phelan 3 years ago
parent
commit
0056933048
  1. 2
      app/controllers/case_logs_controller.rb
  2. 9
      app/models/case_log.rb
  3. 3
      config/forms/2021_2022.json

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

9
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

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

Loading…
Cancel
Save