mirror of
https://github.com/discourse/discourse.git
synced 2025-04-17 12:59:07 +08:00
UX: number site setting validation message (#24303)
Format big numbers validation to easy to read format like 1,000,000
This commit is contained in:
parent
fb2756537d
commit
c0c525056f
@ -1,6 +1,8 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class IntegerSettingValidator
|
class IntegerSettingValidator
|
||||||
|
include ActionView::Helpers::NumberHelper
|
||||||
|
|
||||||
def initialize(opts = {})
|
def initialize(opts = {})
|
||||||
@opts = opts
|
@opts = opts
|
||||||
@opts[:min] = 0 unless @opts[:min].present? || @opts[:hidden]
|
@opts[:min] = 0 unless @opts[:min].present? || @opts[:hidden]
|
||||||
@ -17,11 +19,15 @@ class IntegerSettingValidator
|
|||||||
|
|
||||||
def error_message
|
def error_message
|
||||||
if @opts[:min] && @opts[:max]
|
if @opts[:min] && @opts[:max]
|
||||||
I18n.t("site_settings.errors.invalid_integer_min_max", min: @opts[:min], max: @opts[:max])
|
I18n.t(
|
||||||
|
"site_settings.errors.invalid_integer_min_max",
|
||||||
|
min: number_with_delimiter(@opts[:min]),
|
||||||
|
max: number_with_delimiter(@opts[:max]),
|
||||||
|
)
|
||||||
elsif @opts[:min]
|
elsif @opts[:min]
|
||||||
I18n.t("site_settings.errors.invalid_integer_min", min: @opts[:min])
|
I18n.t("site_settings.errors.invalid_integer_min", min: number_with_delimiter(@opts[:min]))
|
||||||
elsif @opts[:max]
|
elsif @opts[:max]
|
||||||
I18n.t("site_settings.errors.invalid_integer_max", max: @opts[:max])
|
I18n.t("site_settings.errors.invalid_integer_max", max: number_with_delimiter(@opts[:max]))
|
||||||
else
|
else
|
||||||
I18n.t("site_settings.errors.invalid_integer")
|
I18n.t("site_settings.errors.invalid_integer")
|
||||||
end
|
end
|
||||||
|
@ -81,7 +81,7 @@ RSpec.describe IntegerSettingValidator do
|
|||||||
end
|
end
|
||||||
|
|
||||||
context "with min and max" do
|
context "with min and max" do
|
||||||
subject(:validator) { described_class.new(min: -1, max: 3) }
|
subject(:validator) { described_class.new(min: -1, max: 1_000_000) }
|
||||||
|
|
||||||
include_examples "for all IntegerSettingValidator opts"
|
include_examples "for all IntegerSettingValidator opts"
|
||||||
|
|
||||||
@ -92,9 +92,13 @@ RSpec.describe IntegerSettingValidator do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "returns false if value is out of range" do
|
it "returns false if value is out of range" do
|
||||||
expect(validator.valid_value?(4)).to eq(false)
|
expect(validator.valid_value?(1_000_001)).to eq(false)
|
||||||
expect(validator.valid_value?(-2)).to eq(false)
|
expect(validator.valid_value?(-2)).to eq(false)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "returns formatted error" do
|
||||||
|
expect(validator.error_message).to eq("Value must be between -1 and 1,000,000.")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "when setting is hidden" do
|
context "when setting is hidden" do
|
||||||
|
Loading…
x
Reference in New Issue
Block a user