【问题标题】:Customizing Model Validation error messages alerts自定义模型验证错误消息警报
【发布时间】:2013-03-29 01:07:59
【问题描述】:

我正在尝试自定义用户在输入数据不正确时在表单顶部看到的错误消息警报。我尝试自定义的错误消息警报适用于嵌套形式的模型属性。

我尝试了解决方案 here,它说要编辑 config/locales/en.yml 文件,但这只会更改消息,而不是错误消息之前显示的模型和属性的名称。

我还尝试了比利在下面his answer 中的建议,结果相同。即

1 个错误禁止保存此远足路径:
- 来自“我的自定义空白错误消息”的路线指示

有没有办法让我在错误消息中显示对用户更友好的模型和属性名称,或者将它们完全从错误消息中删除?

这是我所拥有的:

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"

型号

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

【问题讨论】:

    标签: ruby-on-rails-3 locale alert nested-forms nested-attributes


    【解决方案1】:

    您可以使用:message 选项来分配自定义错误消息。

    例子:

    validates :directions_from, presence: true, 
      message: "'Direction from' really really really can't be blank!"
    

    那么这个自定义错误信息会在表单视图中显示为&lt;%= msg %&gt;。

    参考:http://edgeguides.rubyonrails.org/active_record_validations.html#message

    添加 回答OP关于评论的问题,即网页中显示的消息不是很友好,显示结果为“Direction from 'Direction from'的方向真的真的不能为空”

    原因是视图模板使用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/Errors(full_message 只不过是attribute 和message 的混合体)

    【讨论】:

    • 谢谢你,Billy,我试过了,但它给出了类似的结果,它显示了新消息,但在用户不友好的模型和属性名称之后,即 Directions Directions from 'Direction from ' 真的真的真的不能为空。尝试此操作后,我更新了我的问题,您知道如何从错误消息警报中删除 Directions Directions from 吗?
    • 顺便说一句,对于任何关注这个问题的人,我不得不稍微修改比利的代码,因为我在验证中有一个if 参数。查看此post 了解更多详情
    • @rossmc,检查我添加的答案。
    • 再次感谢比利,我只是选择了 2,它返回了以下错误警报:[:"directions.directions_from", ["'My Custom Error Message'"]] .我不确定为什么它添加了所有方括号和引号,可能是因为这些是嵌套属性吗?我会再玩一些代码。
    • @rossmc,我没有测试过代码,只是一个想法。我再次检查,使用each 应该能够做到这一点,只需在块中提及errors_array。 rubydoc.info/docs/rails/3.2.8/ActiveModel/Errors:each
    【解决方案2】:

    使用 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-31
      • 1970-01-01
      • 2016-04-18
      • 2016-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-08
      相关资源
      最近更新 更多