mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
Extract the validation of Username format in own class to avoid
complexity in user model object
This commit is contained in:
committed by
Neil Lalonde
parent
3ccfa645a8
commit
84191802df
@@ -374,26 +374,10 @@ class User < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def username_format_validator
|
||||
unless username
|
||||
return errors.add(:username, I18n.t(:'user.username.blank'))
|
||||
validator = UsernameValidator.new(username)
|
||||
unless validator.valid_format?
|
||||
errors.add(:username, validator.error)
|
||||
end
|
||||
|
||||
if username.length < User.username_length.begin
|
||||
return errors.add(:username, I18n.t(:'user.username.short', min: User.username_length.begin))
|
||||
end
|
||||
|
||||
if username.length > User.username_length.end
|
||||
return errors.add(:username, I18n.t(:'user.username.long', max: User.username_length.end))
|
||||
end
|
||||
|
||||
if username =~ /[^A-Za-z0-9_]/
|
||||
return errors.add(:username, I18n.t(:'user.username.characters'))
|
||||
end
|
||||
|
||||
if username[0,1] =~ /[^A-Za-z0-9]/
|
||||
return errors.add(:username, I18n.t(:'user.username.must_begin_with_alphanumeric'))
|
||||
end
|
||||
nil
|
||||
end
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user