FIX: Better error messages when name is too long

Previously you'd get a server side generic error due to a password check
failing. Now the input element has a maxlength attribute and the server
side will respond with a nicer error message if the value is too long.
This commit is contained in:
Robin Ward 2020-02-03 14:12:45 -05:00
parent 31775c996c
commit ee17138c0f
3 changed files with 11 additions and 5 deletions

View File

@ -30,7 +30,7 @@
<label class="control-label">{{i18n 'user.name.title'}}</label>
<div class="controls">
{{#if model.can_edit_name}}
{{text-field value=newNameInput classNames="input-xxlarge"}}
{{text-field value=newNameInput classNames="input-xxlarge" maxlength="255"}}
{{else}}
<span class='static'>{{model.name}}</span>
{{/if}}

View File

@ -1402,10 +1402,11 @@ class User < ActiveRecord::Base
end
def name_validator
if name.present? &&
(confirm_password?(name) || confirm_password?(name&.downcase))
errors.add(:name, :same_as_password)
if name.present?
name_pw = name[0...User.max_password_length]
if confirm_password?(name_pw) || confirm_password?(name_pw.downcase)
errors.add(:name, :same_as_password)
end
end
end

View File

@ -65,6 +65,11 @@ describe User do
expect(user.errors.full_messages.first)
.to include(user_error_message(:name, :same_as_password))
end
it "doesn't raise an error if the name is longer than the max password length" do
user.name = 'x' * 220
expect(user).to be_valid
end
end
describe 'emails' do