mirror of
https://github.com/discourse/discourse.git
synced 2025-09-06 10:50:21 +08:00
FEATURE: allow for a localized error when a regex fails in site settings
FEATURE: apply string validation to list site settings (so we get regex)
This commit is contained in:
parent
ea40dd08e6
commit
0742f340f9
7 changed files with 26 additions and 4 deletions
|
@ -3,6 +3,7 @@
|
||||||
</div>
|
</div>
|
||||||
<div class="setting-value">
|
<div class="setting-value">
|
||||||
{{list-setting settingValue=value choices=choices settingName=setting}}
|
{{list-setting settingValue=value choices=choices settingName=setting}}
|
||||||
|
<div {{bind-attr class=":validation-error validationMessage::hidden"}}><i class='fa fa-times'></i> {{validationMessage}}</div>
|
||||||
<div class='desc'>{{{unbound description}}}</div>
|
<div class='desc'>{{{unbound description}}}</div>
|
||||||
</div>
|
</div>
|
||||||
{{#if dirty}}
|
{{#if dirty}}
|
||||||
|
|
|
@ -1141,6 +1141,7 @@ en:
|
||||||
invalid_integer_max: "Value cannot be higher than %{max}."
|
invalid_integer_max: "Value cannot be higher than %{max}."
|
||||||
invalid_integer: "Value must be an integer."
|
invalid_integer: "Value must be an integer."
|
||||||
regex_mismatch: "Value doesn't match the required format."
|
regex_mismatch: "Value doesn't match the required format."
|
||||||
|
must_include_latest: "Top menu must include the 'latest' tab."
|
||||||
invalid_string: "Invalid value."
|
invalid_string: "Invalid value."
|
||||||
invalid_string_min_max: "Must be between %{min} and %{max} characters."
|
invalid_string_min_max: "Must be between %{min} and %{max} characters."
|
||||||
invalid_string_min: "Must be at least %{min} characters."
|
invalid_string_min: "Must be at least %{min} characters."
|
||||||
|
|
|
@ -88,8 +88,9 @@ basic:
|
||||||
client: true
|
client: true
|
||||||
refresh: true
|
refresh: true
|
||||||
type: list
|
type: list
|
||||||
default: 'latest|new|unread|top|categories'
|
default: "latest|new|unread|top|categories"
|
||||||
regex: "latest"
|
regex: "latest"
|
||||||
|
regex_error: "site_settings.errors.must_include_latest"
|
||||||
choices:
|
choices:
|
||||||
- latest
|
- latest
|
||||||
- new
|
- new
|
||||||
|
|
|
@ -385,7 +385,8 @@ module SiteSettingExtension
|
||||||
'email' => EmailSettingValidator,
|
'email' => EmailSettingValidator,
|
||||||
'username' => UsernameSettingValidator,
|
'username' => UsernameSettingValidator,
|
||||||
types[:fixnum] => IntegerSettingValidator,
|
types[:fixnum] => IntegerSettingValidator,
|
||||||
types[:string] => StringSettingValidator
|
types[:string] => StringSettingValidator,
|
||||||
|
'list' => StringSettingValidator
|
||||||
}
|
}
|
||||||
@validator_mapping[type_name]
|
@validator_mapping[type_name]
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,6 +2,7 @@ class StringSettingValidator
|
||||||
def initialize(opts={})
|
def initialize(opts={})
|
||||||
@opts = opts
|
@opts = opts
|
||||||
@regex = Regexp.new(opts[:regex]) if opts[:regex]
|
@regex = Regexp.new(opts[:regex]) if opts[:regex]
|
||||||
|
@regex_error = opts[:regex_error] || 'site_settings.errors.regex_mismatch'
|
||||||
end
|
end
|
||||||
|
|
||||||
def valid_value?(val)
|
def valid_value?(val)
|
||||||
|
@ -22,7 +23,7 @@ class StringSettingValidator
|
||||||
|
|
||||||
def error_message
|
def error_message
|
||||||
if @regex_fail
|
if @regex_fail
|
||||||
I18n.t('site_settings.errors.regex_mismatch')
|
I18n.t(@regex_error)
|
||||||
elsif @length_fail
|
elsif @length_fail
|
||||||
if @opts[:min] && @opts[:max]
|
if @opts[:min] && @opts[:max]
|
||||||
I18n.t('site_settings.errors.invalid_string_min_max', {min: @opts[:min], max: @opts[:max]})
|
I18n.t('site_settings.errors.invalid_string_min_max', {min: @opts[:min], max: @opts[:max]})
|
||||||
|
|
|
@ -29,6 +29,7 @@ describe SiteSettingExtension do
|
||||||
|
|
||||||
describe "refresh!" do
|
describe "refresh!" do
|
||||||
|
|
||||||
|
|
||||||
it "will reset to default if provider vanishes" do
|
it "will reset to default if provider vanishes" do
|
||||||
settings.setting(:hello, 1)
|
settings.setting(:hello, 1)
|
||||||
settings.hello = 100
|
settings.hello = 100
|
||||||
|
@ -167,6 +168,22 @@ describe SiteSettingExtension do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
describe "string setting with regex" do
|
||||||
|
it "Supports custom validation errors" do
|
||||||
|
settings.setting(:test_str, "bob", regex: "hi", regex_error: "oops")
|
||||||
|
settings.refresh!
|
||||||
|
|
||||||
|
begin
|
||||||
|
settings.test_str = "a"
|
||||||
|
rescue Discourse::InvalidParameters => e
|
||||||
|
message = e.message
|
||||||
|
end
|
||||||
|
|
||||||
|
message.should =~ /oops/
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "bool setting" do
|
describe "bool setting" do
|
||||||
before do
|
before do
|
||||||
settings.setting(:test_hello?, false)
|
settings.setting(:test_hello?, false)
|
||||||
|
|
|
@ -63,7 +63,7 @@ describe SiteSetting do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "top_menu" do
|
describe "top_menu" do
|
||||||
before(:each) { SiteSetting.top_menu = 'one,-nope|two|three,-not|four,ignored|category/xyz' }
|
before { SiteSetting.top_menu = 'one,-nope|two|three,-not|four,ignored|category/xyz|latest' }
|
||||||
|
|
||||||
describe "items" do
|
describe "items" do
|
||||||
let(:items) { SiteSetting.top_menu_items }
|
let(:items) { SiteSetting.top_menu_items }
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue