2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-06-13 06:03:03 +08:00
|
|
|
class IntegerSettingValidator
|
2023-11-10 11:27:35 +08:00
|
|
|
include ActionView::Helpers::NumberHelper
|
|
|
|
|
2014-06-13 06:03:03 +08:00
|
|
|
def initialize(opts = {})
|
|
|
|
@opts = opts
|
2017-04-22 14:36:02 +08:00
|
|
|
@opts[:min] = 0 unless @opts[:min].present? || @opts[:hidden]
|
2017-08-24 22:16:41 +08:00
|
|
|
# set max closer to a long int
|
|
|
|
@opts[:max] = 2_000_000_000 unless @opts[:max].present? || @opts[:hidden]
|
2014-06-13 06:03:03 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def valid_value?(val)
|
|
|
|
return false if val.to_i.to_s != val.to_s
|
|
|
|
return false if @opts[:min] && @opts[:min].to_i > (val.to_i)
|
|
|
|
return false if @opts[:max] && @opts[:max].to_i < (val.to_i)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2014-06-18 22:49:21 +08:00
|
|
|
def error_message
|
2014-06-13 06:03:03 +08:00
|
|
|
if @opts[:min] && @opts[:max]
|
2023-11-10 11:27:35 +08:00
|
|
|
I18n.t(
|
|
|
|
"site_settings.errors.invalid_integer_min_max",
|
|
|
|
min: number_with_delimiter(@opts[:min]),
|
|
|
|
max: number_with_delimiter(@opts[:max]),
|
|
|
|
)
|
2014-06-13 06:03:03 +08:00
|
|
|
elsif @opts[:min]
|
2023-11-10 11:27:35 +08:00
|
|
|
I18n.t("site_settings.errors.invalid_integer_min", min: number_with_delimiter(@opts[:min]))
|
2014-06-13 06:03:03 +08:00
|
|
|
elsif @opts[:max]
|
2023-11-10 11:27:35 +08:00
|
|
|
I18n.t("site_settings.errors.invalid_integer_max", max: number_with_delimiter(@opts[:max]))
|
2014-06-13 06:03:03 +08:00
|
|
|
else
|
|
|
|
I18n.t("site_settings.errors.invalid_integer")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|