translations for custom validation error messages

33 views
Skip to first unread message

Ace Suares

unread,
Jul 20, 2012, 8:21:50 PM7/20/12
to rubyonra...@googlegroups.com
In validations, one can add a custom error message:
 
class Person < ActiveRecord::Base
  def a_method_used_for_validation_purposes
    errors.add(:name, "cannot contain the characters !@#%*()_-+=")
  end
end

How wil this message be translated when using I18n?
Can a key be passed instead of a message?

Cheers,
ace

Norbert Melzer

unread,
Jul 22, 2012, 5:47:11 AM7/22/12
to rubyonra...@googlegroups.com


Am 21.07.2012 02:21 schrieb "Ace Suares" <aces...@gmail.com>:
>
> In validations, one can add a custom error message:
>  
>>
>> class Person < ActiveRecord::Base
>>   def a_method_used_for_validation_purposes

    errors.add(:name, I18n.t(:thekey)) #"cannot contain the characters !@#%*()_-+=")


>>   end
>> end
>
>
> How wil this message be translated when using I18n?
> Can a key be passed instead of a message?

That should do it.

> Cheers,
> ace

Hth
Norbert

Ace Suares

unread,
Jul 23, 2012, 7:31:54 AM7/23/12
to rubyonra...@googlegroups.com
Thanks,

Indeed, when a message is send as a String, it will be taken literally,
and if it is send as a Symbol, it will be looked up in the translation table.

Nifty!

Cheers
ace
Reply all
Reply to author
Forward
0 new messages