2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-08-22 14:49:52 -05:00
|
|
|
class MinUsernameLengthValidator
|
2022-07-08 10:00:47 -05:00
|
|
|
MIN_USERNAME_LENGTH_RANGE = 1..60
|
|
|
|
|
2018-08-22 14:49:52 -05:00
|
|
|
def initialize(opts = {})
|
|
|
|
@opts = opts
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_value?(value)
|
2022-07-08 10:00:47 -05:00
|
|
|
if !MIN_USERNAME_LENGTH_RANGE.cover?(value)
|
|
|
|
@min_range_violation = true
|
|
|
|
return false
|
|
|
|
end
|
2018-08-22 14:49:52 -05:00
|
|
|
return false if value > SiteSetting.max_username_length
|
2023-02-12 22:39:45 -06:00
|
|
|
@username = User.where("length(username) < ?", value).pick(:username)
|
2018-10-02 02:20:19 -05:00
|
|
|
@username.blank?
|
2018-08-22 14:49:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def error_message
|
2022-07-08 10:00:47 -05:00
|
|
|
if @min_length_violation
|
2023-01-09 06:10:19 -06:00
|
|
|
I18n.t(
|
|
|
|
"site_settings.errors.invalid_integer_min_max",
|
|
|
|
min: MIN_USERNAME_LENGTH_RANGE.begin,
|
|
|
|
max: MIN_USERNAME_LENGTH_RANGE.end,
|
|
|
|
)
|
2022-07-08 10:00:47 -05:00
|
|
|
elsif @username.blank?
|
2018-08-22 14:49:52 -05:00
|
|
|
I18n.t("site_settings.errors.min_username_length_range")
|
|
|
|
else
|
|
|
|
I18n.t("site_settings.errors.min_username_length_exists", username: @username)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|