Rails i18n 特定的错误验证格式



因此,您可以使用如下所示的内容更改en.yml中的错误消息:

en:
  activerecord:
    errors:
      models:
        foo:
          attributes:
            amount:
              greater_than_or_equal_to: "Custom GTOE error message."

但是,这将说明以下内容:

Amount Custom GTOE error message.

我知道我可以通过以下方式全局删除它:

en:
  activerecord:
    errors:
      format: "%{message}"

但是我可以仅删除此验证的%{attribute}吗?

谢谢!

我不确定您是否可以从 en.yml 中删除它,但您可以制作一个我认为具有您想要的签名的自定义:

def discount_cannot_be_greater_than_total_value
    if attribute > total_value
      errors.add(:base, "can't be greater than total value")
    end
end
 validates :amount, :numericality => { :greater_than_or_equal_to => YOUR_VALUE, :message => YOUR_ERROR_MESSAGE }

最新更新