自定义模型验证错误消息警报

6

我正在尝试自定义表单顶部用户在输入数据错误时看到的错误消息提示。我想要自定义的错误消息提示是针对嵌套表单中的模型属性的。

我已经尝试了这里提供的解决方案,该方案建议编辑config/locales/en.yml文件,但这仅更改了消息,未更改显示在错误消息之前的模型和属性名称。

我还尝试了Billy在下面提供的建议,但结果相同,即:

1 error prohibited this hikingtrail from being saved:
- Directions directions from 'My Custom Blank Error Message'

是否有一种方法可以在我的错误消息中显示更友好的模型和属性名称,或者从错误消息中完全删除它们?

以下是我的配置:

config/locales/en.yml

    # Sample localization file for English. Add more files in this directory for other locales.
    # See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
en:  
  activerecord:
    models: 
      direction: "In the Getting There section"
    attributes:
      direction:
        directions_from: "From field"
    errors:
      full_messages:
      format: "%{message}"
      models:
        direction:
          attributes:
            directions_from:
              blank: "My Custom Blank Error Message"

Model

class Direction < ActiveRecord::Base
  belongs_to :hikingtrail

  attr_accessible :directions_by, :directions_desc, :directions_from

  validates :directions_from, :presence => {message: "'My Custom Error Message'", :if => Proc.new { |a| a.directions_by? || a.directions_desc? } }

  validates :directions_by, :presence => {message: "'My Custom Error Message'", :if => Proc.new { |a| a.directions_from? || a.directions_desc? } }

  validates :directions_desc, :presence => {message: "'My Custom Error Message'", :if => Proc.new { |a| a.directions_from? || a.directions_by? } }
end
2个回答

6
您可以使用 :message 选项来指定自定义错误消息。
例如:
validates :directions_from, presence: true, 
  message: "'Direction from' really really really can't be blank!"

然后这个自定义错误信息将作为<%= msg %>出现在表单视图中。
参考:http://edgeguides.rubyonrails.org/active_record_validations.html#message 补充回答评论区的问题,即网页显示的消息不太友好,结果显示为“Directions directions from 'Direction from' really really really can't be blank”。
原因是视图模板使用errors.full_messages来显示错误消息。您可以通过两种选项轻松自定义它:
选项1:写一个没有主题的自定义消息。例如:really can't be blank
选项2:像以前一样完整地写句子,但在视图中只引用message,而不是full_message
示例:
<% @hikingtrail.errors.messages.each do |msg| %>
    <li><%= msg %></li>
<% end %>

参考:http://rubydoc.info/docs/rails/3.2.8/ActiveModel/Errorsfull_message 只是 attributemessage 的混合)


1
顺便说一句,对于任何跟进这个问题的人,我不得不稍微修改Billy的代码,因为我的验证中有一个if参数。查看此帖子以获取更多详细信息。 - Holly
1
@rossmc,请查看我添加的答案。 - Billy Chan
再次感谢你,Billy。我刚刚选择了选项2,它返回了以下错误提示:**[:"directions.directions_from", ["'My Custom Error Message'"]]**。我不确定为什么它添加了所有方括号和引号,可能是因为这些是嵌套属性吗?我会再尝试一下代码。 - Holly
我尝试了这个,即<% @hikingtrail.errors.messages.each do |msg, errors_array| %> <li><%= msg %></li> <% end %>,但现在我收到了这个错误信息提示:directions.directions_from - Holly
让我们在聊天中继续这个讨论:http://chat.stackoverflow.com/rooms/27192/discussion-between-billy-chan-and-rossmc - Billy Chan
显示剩余6条评论

0
在Rails 6中,现在可以在模型或属性级别上自定义full_messages助手使用的格式。
en:
  activerecord:
    errors:
      models:
        direction:
          attributes:
            directions_from:
              format: "%{message}"
              blank: "'Direction from' really really really can't be blank!"

这样,针对此错误生成的消息将是您的消息而没有模型前缀,同时保留其他模型和属性的默认full_messages。

更多信息请参见本文: https://blog.bigbinary.com/2019/04/22/rails-6-allows-to-override-the-activemodel-errors-full_message-format-at-the-model-level-and-at-the-attribute-level.html


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接