enforce a maximum username length of 25
This commit is contained in:
parent
7f9c227ed0
commit
c09542a80a
|
@ -27,7 +27,8 @@ class User < ActiveRecord::Base
|
|||
|
||||
validates :password, :presence => true, :on => :create
|
||||
|
||||
validates :username, :format => { :with => /\A[A-Za-z0-9][A-Za-z0-9_-]*\Z/ },
|
||||
validates :username,
|
||||
:format => { :with => /\A[A-Za-z0-9][A-Za-z0-9_-]{0,24}\Z/ },
|
||||
:uniqueness => { :case_sensitive => false }
|
||||
|
||||
validates_each :username do |record,attr,value|
|
||||
|
@ -56,6 +57,12 @@ class User < ActiveRecord::Base
|
|||
end
|
||||
end
|
||||
|
||||
def self.username_regex
|
||||
User.validators_on(:username).select{|v|
|
||||
v.class == ActiveModel::Validations::FormatValidator }.first.
|
||||
options[:with].inspect
|
||||
end
|
||||
|
||||
def as_json(options = {})
|
||||
h = super(:only => [
|
||||
:username,
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
<%= f.label :username, "Username:", :class => "required" %>
|
||||
<%= f.text_field :username, :size => 15 %>
|
||||
<span class="hint">
|
||||
<tt>[A-Za-z0-9][A-Za-z0-9_-]*</tt>
|
||||
<tt><%= User.username_regex %></tt>
|
||||
</span>
|
||||
</div>
|
||||
|
||||
|
|
|
@ -28,7 +28,7 @@
|
|||
<%= f.label :username, "Username:", :class => "required" %>
|
||||
<%= f.text_field :username, :size => 30 %>
|
||||
<span class="hint">
|
||||
<tt>[A-Za-z0-9][A-Za-z0-9_-]*</tt>
|
||||
<tt><%= User.username_regex %></tt>
|
||||
</span>
|
||||
<br />
|
||||
|
||||
|
|
Loading…
Reference in a new issue