Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

[39736] Better password confirmation message

Oliver Günthera93b065971a2
diff --git a/app/models/user.rb b/app/models/user.rb
index c92d606e594..9fceb1fcb05 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -97,21 +97,26 @@ class User < Principal
attr_accessor :password, :password_confirmation, :last_before_login_on
- validates_presence_of :login,
+ validates :login,
:firstname,
:lastname,
:mail,
- unless: Proc.new { |user| user.builtin? }
+ presence: { unless: Proc.new { |user| user.builtin? } }
- validates_uniqueness_of :login, if: Proc.new { |user| !user.login.blank? }, case_sensitive: false
- validates_uniqueness_of :mail, allow_blank: true, case_sensitive: false
+ validates :login, uniqueness: { if: Proc.new { |user| !user.login.blank? }, case_sensitive: false }
+ validates :mail, uniqueness: { allow_blank: true, case_sensitive: false }
# Login must contain letters, numbers, underscores only
- validates_format_of :login, with: /\A[a-z0-9_\-@.+ ]*\z/i
- validates_length_of :login, maximum: 256
- validates_length_of :firstname, :lastname, maximum: 256
+ validates :login, format: { with: /\A[a-z0-9_\-@.+ ]*\z/i }
+ validates :login, length: { maximum: 256 }
+ validates :firstname, :lastname, length: { maximum: 256 }
validates :mail, email: true, unless: Proc.new { |user| user.mail.blank? }
- validates_length_of :mail, maximum: 256, allow_nil: true
- validates_confirmation_of :password, allow_nil: true
+ validates :mail, length: { maximum: 256, allow_nil: true }
+
+ validates :password,
+ confirmation: {
+ allow_nil: true,
+ message: ->(*) { I18n.t('activerecord.errors.models.user.attributes.password_confirmation.confirmation') }
+ }
auto_strip_attributes :login, nullify: false
auto_strip_attributes :mail, nullify: false
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 9dd250b9dee..df762ff26bf 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -806,6 +806,9 @@ en:
group_without_name: "Unnamed groups are not allowed."
user:
attributes:
+ password_confirmation:
+ confirmation: "Password confirmation does not match password."
+ format: "%{message}"
password:
weak: "Must contain characters of the following classes (at least %{min_count} of %{all_count}): %{rules}."
lowercase: "lowercase (e.g. 'a')"
diff --git a/spec/controllers/my_controller_spec.rb b/spec/controllers/my_controller_spec.rb
index 11e25275a77..e35acc0c1dc 100644
--- a/spec/controllers/my_controller_spec.rb
+++ b/spec/controllers/my_controller_spec.rb
@@ -71,7 +71,8 @@ describe MyController, type: :controller do
assert_response :success
assert_template 'password'
expect(user.errors.attribute_names).to eq([:password_confirmation])
- expect(user.errors.map(&:message).flatten.join('')).to include("doesn't match")
+ expect(user.errors.map(&:message).flatten)
+ .to contain_exactly("Password confirmation does not match password.")
end
end
diff --git a/spec/features/users/self_registration_spec.rb b/spec/features/users/self_registration_spec.rb
index 71d02bdbc22..ccdfee231f7 100644
--- a/spec/features/users/self_registration_spec.rb
+++ b/spec/features/users/self_registration_spec.rb
@@ -80,7 +80,7 @@ describe 'user self registration', type: :feature, js: true do
end
expect(page)
- .to have_content('Confirmation doesn\'t match Password')
+ .to have_content('Password confirmation does not match password.')
# correcting password
within '.registration-modal' do