How to change validation messages?

28 views
Skip to first unread message

Anton Kuzmin

unread,
Apr 19, 2015, 1:46:09 PM4/19/15
to plataforma...@googlegroups.com
For example, I need to change "can't be blank" message.

Devise's Validatable module ads this to the model:
validates_presence_of   :email, if: :email_required?

What I actually need to do:
validates :email, presence: { message: lambda { |error, attributes| I18n.t('forms.cannot_be_blank.fem') }}

Lucas Mazza

unread,
Apr 19, 2015, 1:59:31 PM4/19/15
to plataforma...@googlegroups.com
You can use I18n for that, just scope the translation for the proper model + attribute http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models

--

---
You received this message because you are subscribed to the Google Groups "Devise" group.
To unsubscribe from this group and stop receiving emails from it, send an email to plataformatec-de...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Anton Kuzmin

unread,
Apr 19, 2015, 2:01:18 PM4/19/15
to plataforma...@googlegroups.com
Yes, but the problem is to override the validation that devise provided. When I just add another "validates" call to my user model, I get 2 messages about blank email.
You received this message because you are subscribed to a topic in the Google Groups "Devise" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/plataformatec-devise/lZCaLMaCCUY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to plataformatec-de...@googlegroups.com.

Lucas Mazza

unread,
Apr 19, 2015, 2:03:53 PM4/19/15
to plataforma...@googlegroups.com
If you are rolling your own validations then you shouldn't use the validatable module. If all you want is to simply translate the messages, without changing any of the existing behaviour of the validations, you can change the messages through i18n. But if your models require a more complex validation scheme then it's recommended to remove the validatable module and roll your own validations.

Anton Kuzmin

unread,
Apr 19, 2015, 2:18:22 PM4/19/15
to plataforma...@googlegroups.com
Thank you, Lucas.

I think I need to roll my own Validatable module because different fields need different "can't be blank" messages depending on grammar rules of the target language.
Reply all
Reply to author
Forward
0 new messages