Rails 3 自定义格式验证错误?
使用这个模型:
validates_presence_of :email, :message => "We need your email address"
作为一个相当人为的例子。错误显示为:
Email We need your email address
我如何自己提供格式?
我查看了 ActiveModel::Errors#full_messages
的源代码,它执行以下操作:
def full_messages
full_messages = []
each do |attribute, messages|
messages = Array.wrap(messages)
next if messages.empty?
if attribute == :base
messages.each {|m| full_messages << m }
else
attr_name = attribute.to_s.gsub('.', '_').humanize
attr_name = @base.class.human_attribute_name(attribute, :default => attr_name)
options = { :default => "%{attribute} %{message}", :attribute => attr_name }
messages.each do |m|
full_messages << I18n.t(:"errors.format", options.merge(:message => m))
end
end
end
full_messages
end
注意到选项中的 :default
格式字符串吗?所以我尝试了:
validates_presence_of :email, :message => "We need your email address", :default => "something"
但错误消息实际上显示为:
Email something
然后我尝试包含插值字符串 %{message}
,从而覆盖 %{attribute} %{message}
Rails 默认使用的版本。这会导致异常:
SubscriptionsController#create 中的 I18n::MissingInterpolationArgument
给定的“%{message}”({:model=>"Subscription", :attribute=>"Email", :value=>""} 中缺少插值参数
然而如果我使用插值字符串 %{attribute}
,它不会出错,它只会两次输出人性化的属性名称。
有人有这方面的经验吗?通常我们需要一些其他字符串(营销人员总是让事情变得更复杂!)。
With this model:
validates_presence_of :email, :message => "We need your email address"
as a rather contrived example. The error comes out as:
Email We need your email address
How can I provide the format myself?
I looked at the source code of ActiveModel::Errors#full_messages
and it does this:
def full_messages
full_messages = []
each do |attribute, messages|
messages = Array.wrap(messages)
next if messages.empty?
if attribute == :base
messages.each {|m| full_messages << m }
else
attr_name = attribute.to_s.gsub('.', '_').humanize
attr_name = @base.class.human_attribute_name(attribute, :default => attr_name)
options = { :default => "%{attribute} %{message}", :attribute => attr_name }
messages.each do |m|
full_messages << I18n.t(:"errors.format", options.merge(:message => m))
end
end
end
full_messages
end
Notice the :default
format string in the options? So I tried:
validates_presence_of :email, :message => "We need your email address", :default => "something"
But then the error message actually appears as:
Email something
So then I tried including the interpolation string %{message}
, thus overriding the %{attribute} %{message}
version Rails uses by default. This causes an Exception:
I18n::MissingInterpolationArgument in SubscriptionsController#create
missing interpolation argument in "%{message}" ({:model=>"Subscription", :attribute=>"Email", :value=>""} given
Yet if I use the interpolation string %{attribute}
, it doesn't error, it just spits out the humanized attribute name twice.
Anyone got any experience with this? I could always have the attribute name first, but quite often we need some other string (marketing guys always make things more complicated!).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
:base
上的错误并不特定于任何属性,因此人性化的属性名称不会附加到消息中。这允许我们添加有关电子邮件的错误消息,但不将它们附加到电子邮件属性,并获得预期的结果:Errors on
:base
are not specific to any attribute, so the humanized attribute name is not appended to the message. This allows us to add error messages about email, but not attach them to the email attribute, and get the intended result:为此使用国际化可能是您最好的选择。看看
http://guides.rubyonrails.org/i18n .html#translations-for-active-record-models
特别是这一部分:
Using internationalization for this is probably your best bet. Take a look at
http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models
Particularly this section: