i18n simplebackend AR custom messages on edge

Skip to first unread message

Claudio Poli

Aug 28, 2008, 3:59:20 AM8/28/08
to rails-i18n
hi everyone,
thanks for the effort in putting i18n framework into rails.

I got a problem, everything works fine until I set a custom error
message for a validation, for example

validates_presence_of :patient_uuid, :message
=> :can_t_be_blank_or_is_not_valid

this is a part of the language file, stripped down

can_t_be_blank_or_is_not_valid: "can't be blank or is not

however during validation the only error that shows up is the one
defined in blank: "can't be blank", which is default for
validates_presence_of .

is my approach wrong?


Sven Fuchs

Aug 28, 2008, 5:56:54 AM8/28/08
to rails...@googlegroups.com
Hi Claudio,

thanks for posting this.

I think we have a bug here resulting from the recent changes to the
translation scopes. When I add this to Karel's demo app and output the
scopes that are looked up I get something like this:

", :scope
, :errors
], :default
.book.blank", :can_t_be_blank_or_is_not_valid, :"messages.blank"]}

That is, the custom key is only used once. Shouldn't it be used on the
models.book and models.book.attributes scope as well?

Also, it is looked up as is without being scoped to :messages, so it
won't be found at all.

Iain, are you around? What do you think?

From a first glance, I'd think in this case the keys looked up should

:"models.book.attributes.title. can_t_be_blank_or_is_not_valid"
:"models.book. can_t_be_blank_or_is_not_valid",

Or am I wrong?
sven fuchs sven...@artweb-design.de
artweb design http://www.artweb-design.de
grünberger 65 + 49 (0) 30 - 47 98 69 96 (phone)
d-10245 berlin + 49 (0) 171 - 35 20 38 4 (mobile)


Aug 28, 2008, 4:07:45 PM8/28/08
to rails-i18n
Ah, something I did! ;)

My first run through what is exactly happening here:

The problem is that users do thing exactly opposite to the default
When you're writing a custom validation:

validate :my_validation
def my_validation
errors.add(:title, :can_t_be_blank_or_is_not_valid) if

But the built in validations do:

errors.add(:title, :blank, :default
=> :can_t_be_blank_or_is_not_valid)

Since I handled a errors.add the same way as I18n.translate, with the
default as last resort, this fails horribly.

I could do two things:

1. Make the user enter custom validations only using a default way,
making the second argument to errors.add practically useless. # =>
errors.add(:title, 'only show me when no translations are
present', :default => :can_t_be_blank_or_is_not_valid)
2. Make all the built-in validations behave different under water :S
3. Find out a way where both keys have meaning, but it would mean more
complexity in an already complex method.

Okay, not really 2.

I am not really sure on how to continue with this.

I think errors.add(:title, :can_t_be_blank_or_is_not_valid, :default
=> "show me only when no translations are present") is the right way
to do things. But it means redoing a lot of the rails validations.

What are your opinions??

On the topic of scoping: my bad, if it isn't in attributes or models,
it should go to messages.


PS. What a lame key 'can_t_be_blank_or_is_not_valid' ;)
> sven fuchs              svenfu...@artweb-design.de

Redd Vinylene

Aug 29, 2008, 4:15:16 AM8/29/08
to rails...@googlegroups.com
Must ' default to underscore, can't it just be taken away?


This might make no sense to y'all, but what about using Unidecode


Aug 29, 2008, 3:36:34 PM8/29/08
to rails-i18n
So, I have this now:

class Sti < Topic
validates_presence_of :title, :message
=> :can_t_be_blank_or_is_not_valid

produces these key lookup in the scope activerecord.errors:

[ :"models.sti.attributes.title.can_t_be_blank_or_is_not_valid",
:"messages.blank" ]

This is what Sven, Josh and I discussed yesterday as the best

Also, consider this scenario:

class Topic < ActiveRecord::Base
validate :title_cant_fail
def title_cant_fail
errors.add(:title, :cant_fail, :default => "can't fail")

looks up these keys:

[ :"models.topic.attributes.title.cant_fail",
:"messages.cant_fail" ]

but since none of these are specified, it'll just give you the default

ActiveRecord::RecordInvalid: Validation failed: Title can't fail

I'll just need to wrap it up, do some sanity checks on the code, and
then it'll be in a patch soon.

Reply all
Reply to author
0 new messages