diff --git a/app/assets/javascripts/admin/templates/site_settings/setting_list.hbs b/app/assets/javascripts/admin/templates/site_settings/setting_list.hbs index 802a53324eb..2aadfd9845d 100644 --- a/app/assets/javascripts/admin/templates/site_settings/setting_list.hbs +++ b/app/assets/javascripts/admin/templates/site_settings/setting_list.hbs @@ -3,6 +3,7 @@
{{list-setting settingValue=value choices=choices settingName=setting}} +
{{validationMessage}}
{{{unbound description}}}
{{#if dirty}} diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index fe859f71128..f941e7f45ea 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -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." diff --git a/config/site_settings.yml b/config/site_settings.yml index 22866a32e58..85c043ec062 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -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 diff --git a/lib/site_setting_extension.rb b/lib/site_setting_extension.rb index 69538720f7a..fab32a7136a 100644 --- a/lib/site_setting_extension.rb +++ b/lib/site_setting_extension.rb @@ -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 diff --git a/lib/validators/string_setting_validator.rb b/lib/validators/string_setting_validator.rb index 4f7e9a054bb..07a65500bdd 100644 --- a/lib/validators/string_setting_validator.rb +++ b/lib/validators/string_setting_validator.rb @@ -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]}) diff --git a/spec/components/site_setting_extension_spec.rb b/spec/components/site_setting_extension_spec.rb index 8173ba5e303..c8d7b8210f7 100644 --- a/spec/components/site_setting_extension_spec.rb +++ b/spec/components/site_setting_extension_spec.rb @@ -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) diff --git a/spec/models/site_setting_spec.rb b/spec/models/site_setting_spec.rb index ced13c11f54..e1466a40006 100644 --- a/spec/models/site_setting_spec.rb +++ b/spec/models/site_setting_spec.rb @@ -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 }