Submit social housing lettings and sales data (CORE)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.1 KiB

3 years ago
class CaseLogValidator < ActiveModel::Validator
# Methods need to be named 'validate_' followed by field name
3 years ago
# this is how the metaprogramming of the method name being
# call in the validate method works.
def validate_tenant_code(record)
if record.tenant_code.blank?
record.errors.add :tenant_code, "Tenant code can't be blank"
end
end
def validate_tenant_age(record)
if record.tenant_age.blank?
record.errors.add :tenant_age, "Tenant age can't be blank"
3 years ago
elsif !/^[1-9][0-9]?$|^100$/.match?(record.tenant_age.to_s)
record.errors.add :tenant_age, "Tenant age must be between 0 and 100"
3 years ago
end
end
def validate(record)
question_to_validate = options[:previous_page]
if respond_to?("validate_#{question_to_validate}")
public_send("validate_#{question_to_validate}", record)
end
3 years ago
end
end
3 years ago
class CaseLog < ApplicationRecord
validate :instance_validations
attr_writer :previous_page
3 years ago
3 years ago
enum status: { "in progress" => 0, "submitted" => 1 }
def instance_validations
validates_with CaseLogValidator, ({ previous_page: @previous_page } || {})
end
3 years ago
end