2019-11-24 18:49:27 -06:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class TimezoneValidator < ActiveModel::EachValidator
|
|
|
|
def self.valid?(value)
|
|
|
|
ok = ActiveSupport::TimeZone[value].present?
|
|
|
|
Rails.logger.warn("Invalid timezone '#{value}' detected!") if !ok
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.error_message(value)
|
|
|
|
I18n.t("errors.messages.invalid_timezone", tz: value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_each(record, attribute, value)
|
|
|
|
return if value.blank? || TimezoneValidator.valid?(value)
|
2023-01-09 06:10:19 -06:00
|
|
|
record.errors.add(attribute, :timezone, message: TimezoneValidator.error_message(value))
|
2019-11-24 18:49:27 -06:00
|
|
|
end
|
|
|
|
end
|