Rails override validator message

Posted by Anand on Stack Overflow See other posts from Stack Overflow or by Anand
Published on 2010-04-16T10:30:26Z Indexed on 2010/04/16 10:33 UTC
Read the original article Hit count: 325

Filed under:
|
|

Hi, I have a site thats served in 2 flavours, English and French. Here's some code

app/views/user/register.html.erb
-----------------          
<% form_for .....>
  <%= f.text_field :first_name %> 
<% end %>

app/models/user.rb
------------------
class User < ActiveRecord::Base
  validates_presence_of :first_name

end 

Now to display the error message in case if the site is being served in the French version, I have

app/config/locales/fr.yml
-------------------------
  activerecord:
    errors:
      messages:
        empty: "ne peut pas être vide"

So if someone does not fill in a first name, the validator takes the name of the field and appends the custom message for empty clause giving

"First name ne peut pas être vide"

which is incorrect, coz 'First name' in French is 'Prénom', hence it should be

"Prénom ne peut pas être vide"

Please can someone suggest a way of achieving the desired result.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about validation