A platform for community discussion. Free, open, simple.
UX: Improve error messages for minimum and maximum username lengths.
Bianca Nenciue0d7cdac12a1
diff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex 42b27f715df..4d41f7fdb2b 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -163,10 +163,6 @@ en:invalid_choice:one: 'You specified the invalid choice %{name}'other: 'You specified the invalid choices %{name}'- min_username_length_exists: "You cannot set the minimum username length above the shortest username."- min_username_length_range: "You cannot set the minimum above the maximum."- max_username_length_exists: "You cannot set the maximum username length below the longest username."- max_username_length_range: "You cannot set the maximum below the minimum."default_categories_already_selected: "You cannot select a category used in another list."s3_upload_bucket_is_required: "You cannot enable uploads to S3 unless you've provided the 's3_upload_bucket'."conflicting_google_user_id: 'The Google Account ID for this account has changed; staff intervention is required for security reasons. Please contact staff and point them to <br><a href="https://meta.discourse.org/t/76575">https://meta.discourse.org/t/76575</a>'@@ -1839,6 +1835,10 @@ en:reply_by_email_disabled: "You must first enable 'reply by email' before enabling this setting."sso_url_is_empty: "You must set a 'sso url' before enabling this setting."enable_local_logins_disabled: "You must first enable 'enable local logins' before enabling this setting."+ min_username_length_exists: "You cannot set the minimum username length above the shortest username (%{username})."+ min_username_length_range: "You cannot set the minimum above the maximum."+ max_username_length_exists: "You cannot set the maximum username length below the longest username (%{username})."+ max_username_length_range: "You cannot set the maximum below the minimum."search:within_post: "#%{post_number} by %{username}"diff --git a/config/site_settings.yml b/config/site_settings.ymlindex 4443529d79d..921037cfe0d 100644--- a/config/site_settings.yml+++ b/config/site_settings.yml@@ -375,11 +375,13 @@ users:default: 3min: 1max: 60+ validator: "MinUsernameLengthValidator"max_username_length:client: truedefault: 20min: 8max: 60+ validator: "MaxUsernameLengthValidator"reserved_usernames:type: listlist_type: compactdiff --git a/lib/site_settings/validations.rb b/lib/site_settings/validations.rbindex 0643ab49b6a..ae38285cc82 100644--- a/lib/site_settings/validations.rb+++ b/lib/site_settings/validations.rb@@ -5,16 +5,6 @@ module SiteSettings::Validationsraise Discourse::InvalidParameters.new(I18n.t("errors.site_settings.#{key}"))end- def validate_min_username_length(new_val)- validate_error :min_username_length_range if new_val > SiteSetting.max_username_length- validate_error :min_username_length_exists if User.where('length(username) < ?', new_val).exists?- end-- def validate_max_username_length(new_val)- validate_error :min_username_length_range if new_val < SiteSetting.min_username_length- validate_error :max_username_length_exists if User.where('length(username) > ?', new_val).exists?- end-def validate_default_categories(new_val, default_categories_selected)validate_error :default_categories_already_selected if (new_val.split("|").to_set & default_categories_selected).size > 0enddiff --git a/lib/validators/max_username_length_validator.rb b/lib/validators/max_username_length_validator.rbnew file mode 100644index 00000000000..e4668feac5b--- /dev/null+++ b/lib/validators/max_username_length_validator.rb@@ -0,0 +1,19 @@+class MaxUsernameLengthValidator+ def initialize(opts = {})+ @opts = opts+ end++ def valid_value?(value)+ return false if value < SiteSetting.min_username_length+ @username = User.where('length(username) > ?', value).pluck(:username).first+ @username.blank? ? true : false+ end++ def error_message+ if @username.blank?+ I18n.t("site_settings.errors.max_username_length_range")+ else+ I18n.t("site_settings.errors.max_username_length_exists", username: @username)+ end+ end+enddiff --git a/lib/validators/min_username_length_validator.rb b/lib/validators/min_username_length_validator.rbnew file mode 100644index 00000000000..43548185e4f--- /dev/null+++ b/lib/validators/min_username_length_validator.rb@@ -0,0 +1,19 @@+class MinUsernameLengthValidator+ def initialize(opts = {})+ @opts = opts+ end++ def valid_value?(value)+ return false if value > SiteSetting.max_username_length+ @username = User.where('length(username) < ?', value).pluck(:username).first+ @username.blank? ? true : false+ end++ def error_message+ if @username.blank?+ I18n.t("site_settings.errors.min_username_length_range")+ else+ I18n.t("site_settings.errors.min_username_length_exists", username: @username)+ end+ end+enddiff --git a/spec/components/validators/max_username_length_validator_spec.rb b/spec/components/validators/max_username_length_validator_spec.rbnew file mode 100644index 00000000000..dd7751fee62--- /dev/null+++ b/spec/components/validators/max_username_length_validator_spec.rb@@ -0,0 +1,26 @@+require 'rails_helper'++describe MaxUsernameLengthValidator do+ it "checks for minimum range" do+ SiteSetting.min_username_length = 6++ validator = described_class.new+ expect(validator.valid_value?(5)).to eq(false)+ expect(validator.error_message).to eq(I18n.t("site_settings.errors.max_username_length_range"))+ end++ it "checks for users with short usernames" do+ user = Fabricate(:user, username: 'jackjackjack')++ validator = described_class.new+ expect(validator.valid_value?(12)).to eq(true)++ validator = described_class.new+ expect(validator.valid_value?(11)).to eq(false)++ expect(validator.error_message).to eq(I18n.t(+ "site_settings.errors.max_username_length_exists",+ username: 'jackjackjack'+ ))+ end+enddiff --git a/spec/components/validators/min_username_length_validator_spec.rb b/spec/components/validators/min_username_length_validator_spec.rbnew file mode 100644index 00000000000..6a2a4e131af--- /dev/null+++ b/spec/components/validators/min_username_length_validator_spec.rb@@ -0,0 +1,26 @@+require 'rails_helper'++describe MinUsernameLengthValidator do+ it "checks for maximum range" do+ SiteSetting.max_username_length = 10++ validator = described_class.new+ expect(validator.valid_value?(11)).to eq(false)+ expect(validator.error_message).to eq(I18n.t("site_settings.errors.min_username_length_range"))+ end++ it "checks for users with short usernames" do+ user = Fabricate(:user, username: 'jack')++ validator = described_class.new+ expect(validator.valid_value?(4)).to eq(true)++ validator = described_class.new+ expect(validator.valid_value?(5)).to eq(false)++ expect(validator.error_message).to eq(I18n.t(+ "site_settings.errors.min_username_length_exists",+ username: 'jack'+ ))+ end+end