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:
Sam 2015-02-27 11:45:56 +11:00
parent ea40dd08e6
commit 0742f340f9
7 changed files with 26 additions and 4 deletions

View File

@ -3,6 +3,7 @@
</div>
<div class="setting-value">
{{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>
{{#if dirty}}

View File

@ -1141,6 +1141,7 @@ en:
invalid_integer_max: "Value cannot be higher than %{max}."
invalid_integer: "Value must be an integer."
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_min_max: "Must be between %{min} and %{max} characters."
invalid_string_min: "Must be at least %{min} characters."

View File

@ -88,8 +88,9 @@ basic:
client: true
refresh: true
type: list
default: 'latest|new|unread|top|categories'
default: "latest|new|unread|top|categories"
regex: "latest"
regex_error: "site_settings.errors.must_include_latest"
choices:
- latest
- new

View File

@ -385,7 +385,8 @@ module SiteSettingExtension
'email' => EmailSettingValidator,
'username' => UsernameSettingValidator,
types[:fixnum] => IntegerSettingValidator,
types[:string] => StringSettingValidator
types[:string] => StringSettingValidator,
'list' => StringSettingValidator
}
@validator_mapping[type_name]
end

View File

@ -2,6 +2,7 @@ class StringSettingValidator
def initialize(opts={})
@opts = opts
@regex = Regexp.new(opts[:regex]) if opts[:regex]
@regex_error = opts[:regex_error] || 'site_settings.errors.regex_mismatch'
end
def valid_value?(val)
@ -22,7 +23,7 @@ class StringSettingValidator
def error_message
if @regex_fail
I18n.t('site_settings.errors.regex_mismatch')
I18n.t(@regex_error)
elsif @length_fail
if @opts[:min] && @opts[:max]
I18n.t('site_settings.errors.invalid_string_min_max', {min: @opts[:min], max: @opts[:max]})

View File

@ -29,6 +29,7 @@ describe SiteSettingExtension do
describe "refresh!" do
it "will reset to default if provider vanishes" do
settings.setting(:hello, 1)
settings.hello = 100
@ -167,6 +168,22 @@ describe SiteSettingExtension do
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
before do
settings.setting(:test_hello?, false)

View File

@ -63,7 +63,7 @@ describe SiteSetting do
end
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
let(:items) { SiteSetting.top_menu_items }