8

Possible Duplicate:
Fully custom validation error message with Rails

I'm playing around with Rails 3.1rc1 and think SecurePassword will be useful for me. But, I don't like the default error message, Password digest can't be blank. If I called validates_presence_of :password_digest myself I could pass :message => "Password can't be blank" but because it's in the framework I'm not sure how to override the message to remove the word "digest" which will only confuse joe user. Anyone know how to do this?

Edit:

Tried adding an 'overriding': validates_presence_of like so:

class User < ActiveRecord::Base
  attr_accessible :email, :password

  has_secure_password
  validates_presence_of :password_digest, :message => "Password can't be blank"
end

But when trying to submit a blank password you just get double the errors:

Form is invalid

  • Password digest can't be blank
  • Password digest Password can't be blank
Community
  • 1
  • 1
Meltemi
  • 37,979
  • 50
  • 195
  • 293

2 Answers2

20

I believe you can use the Rails Internationalization API to change this.

In your config/locales/en.yml file, add the following:

en:
  activerecord:
    attributes:
      user:
        password_digest: "Password"

If your model class is something other than 'user', you'll need to change that line appropriately.

Anyway, this works for me.

bloomdido
  • 200
  • 5
  • 2
    Doesn't work if you are using mongoid + mongodb, which doesn't (obviously) use active record, any ideas for what you would do in that scenario? – Tyler Brock Oct 22 '11 at 20:25
  • @TylerBrock Take a look at the Translator gem from Hubert Łępicki / amerbit: https://github.com/amberbit/translator – brandonjp Jan 04 '12 at 16:42
  • 1
    @TylerBrock for mongoid, you can just substitute the `activerecord:` bit in the i18n file with `mongoid:` – venables Apr 22 '12 at 02:43
  • Just wanted to say thanks... FROM THE FUTURE! – Adam Templeton Sep 14 '12 at 19:43
  • You don't need to touch any YAML files here nor do you need `validates_presence_of :password_digest`. The password_digest will automatically be validated by `has_secure_password`. Just add `validates :password, :presence => true` and leave out the rest. That worked best for me. – Tintin81 Feb 12 '13 at 16:56
0

All this is doing is adding the lines

validates_confirmation_of :password
validates_presence_of     :password_diges

t

If you add the line

validates_presence_of :password_digest, :message => "Password can't be blank"

That should work, since that is just what calling has_secure_password does.

Devin M
  • 9,636
  • 2
  • 33
  • 46
  • Yeah, I tried this thinking it would override the `validates_presence_of :password_digest` in the framework...but instead just doubles the error messages (see above). – Meltemi May 28 '11 at 17:20
  • Why dont you implement https://github.com/rails/rails/blob/master/activemodel/lib/active_model/secure_password.rb in your app? I just dont really see a way to overide this. – Devin M May 28 '11 at 23:12