【问题标题】:ActiveRecord validates... custom field nameActiveRecord 验证...自定义字段名称
【发布时间】:2011-02-01 01:24:21
【问题描述】:

我想修正我的网站生成的一些错误消息。问题来了:

class Brand < ActiveRecord::Base
    validates_presence_of :foo
    ...
end

我的目标是发出一条消息“需要票证描述”而不是“需要 Foo”或者不能为空,或其他任何内容。

这很重要的原因是因为我们之前说该字段是ticket_summary。这很好,服务器被编码为使用它,但现在由于疯狂的业务分析师已经确定ticket_summary 是一个糟糕的名字,应该是ticket_description。现在我不一定想让我的数据库由用户对字段名称的要求来驱动,特别是因为它们可以经常更改而无需更改功能。

是否已经有提供此功能的机制?

澄清

:message => 似乎不是正确的解决方案, :message 会给我“Foo [message]”作为错误,我希望更改生成的消息字段名称,而不是实际的消息本身(尽管我将满足于不得不改变整个事情)。

【问题讨论】:

    标签: ruby-on-rails activerecord


    【解决方案1】:

    将此添加到您的 config/locales/en.yml 文件中:

    en:
      activerecord:
        errors:
    
          # global message format 
          format: #{message}
    
          full_messages:
            # shared message format across models
            foo:
              blank: Ticket description is required
    
            # model specific message format
            brand:
              zoo:
                blank: Name is required
    

    现在更改您的验证消息以引用新的消息格式:

    validates_presence_of :bar, :message => "Empty bar is not a good idea"
    validates_presence_of :foo, :message => "foo.blank"
    validates_presence_of :zoo, :message => "brand.zoo.blank"
    

    让我们试试代码:

    b = Brand.new
    b.valid?
    b.errors.full_messages
    #=> ["Ticket description is required", 
    #     "Empty bar is not a good idea",
    #     "Name is required"]
    

    如上所示,您可以自定义三个级别的错误消息格式。

    1) 全局用于所有 ActiveRecord 错误消息

      activerecord:
        errors:
          format: #{message}
    

    2) 跨模型共享错误消息

      activerecord:
        errors:
          full_messages:
            foo:
              blank: Ticket description is required
    

    3) 模型特定消息

      activerecord:
        errors:
          full_messages:
            brand:
              zoo:
                blank: Name is required
    

    【讨论】:

    • 这不是问题。消息将是“+Foo+ 这是我的自定义错误消息”。我需要 foo 字段的“+Bar+ 这是我的自定义错误消息”。
    • 我已经更新了我的答案,这应该可以满足您的需求。看看吧。
    • 查看我的答案以获得更简单的解决方案:P
    【解决方案2】:

    所以答案很简单……

    定义

    self.human_attribute_name(attribute) 并返回人类可读的名称:

    def self.human_attribute_name(attribute)
        if attribute == :foo 
            return 'bar'
        end
    end
    

    我当然会使用名称地图。就是这样。

    【讨论】:

    • 这是一个很好的解决方案。我的解决方案非常通用,它允许您在语言环境文件而不是 ruby​​ 代码中更改消息及其格式。
    • 如果属性不是 :foo,你不需要在最后调用 super 吗?
    • 很好,但是这个方法在 rails 4 中有 2 个参数,像这样: def self.human_attribute_name(attribute_key_name, options = {}) case attribute_key_name when :FOO return "Bar" else return super(attribute_key_name , 选项)结束结束
    【解决方案3】:

    您可以按照以下方式进行:

    # config/locales/en.yml
    en:
      activerecord:
        attributes:
          brand:
            foo: "Ticket description"
        errors:
          models:
            brand:
              attributes:
                foo:
                  blank: " is required"
    

    详情请查看Fully custom validation error message with Rails

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-09
      • 1970-01-01
      • 2015-12-20
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多