44

我有带有特殊验证的属性,我使用 message 子句来显示仅用于该验证的特殊消息。这是一个例子:

validates :email, presence:   true, length: { maximum: 60 },
                format:     { with: valid_email_regex, message: "is not a valid email address format." },
                uniqueness: { case_sensitive: false } 

我想在这里翻译消息,但我不知道该怎么做。

我见过他们输入如下内容的示例:message: t("some_value_here"). 我不确定这个名称。我试过这样的消息:t(:bad_email)。我在我的 yaml 文件中执行以下操作只是为了尝试一些东西。

activemodel:
  errors:
    bad_email: "is not a valid email address format."

当我尝试访问我的 Rails 应用程序时,出现以下错误:

ActionView::Template::Error (undefined method `t' for #<Class:0x007fefc1b709e0>)

我也在我的 yaml 文件中尝试了这个:

activemodel:
  errors:
    user:
      bad_email: "is not a valid email address format."

我整天都在研究这个。我能找到的只是替换内置的错误哈希,如空白或空。有没有办法让我拥有自定义错误哈希并在模型中替换它们?在这一点上,我无法让 t 按编码工作。我希望问题在于我如何设置我的 yaml 文件。我已经看到了如何设置它的不同版本。我不确定是否应该将它放在 activemodel 或 activerecord 下。我假设为 activemodel,因为那是我要翻译的自定义消息的位置。

任何帮助,将不胜感激。这是我在启动我的第一个应用程序翻译之前需要弄清楚的最后一块。

更新 2013 年 7 月 29 日下午 7:30 CDT

bgates给了我一个很好的开始,让我了解如何设置我的模型文件以接收 YAML 文件中的自定义消息。但是,我最终不得不在我的 yaml 文件中进行以下设置才能找到自定义消息。

activerecord:
  errors: 
    models: 
      user: 
        attributes: 
          bio: 
            no_links: "cannot contain email addresses or website links (URLs)."
          email: 
            bad_email: "is not a valid email address format."
          username: 
            bad_username: "can only contain numbers and letters.  No special characters or spaces."
4

4 回答 4

86

使用符号表示消息:

validates :email, presence:   true, length: { maximum: 60 },
            format:     { with: valid_email_regex, message: :bad_email },
            uniqueness: { case_sensitive: false } 

然后在yaml文件中

[lang]:
  activerecord:
    errors:
      messages:
        bad_email: "just ain't right"

如果有特定于此模型的翻译,它将覆盖上面的一般翻译:

[lang]:
  activerecord:
    errors:
      models:
        model_name: # or namespace/model_name
          attributes:
            email:
              bad_email: "model-specific message for invalid email"

如果您编写自定义验证,add_error(:email, :bad_email)将执行上面的查找,但errors[:email] << :bad_email不会。

于 2013-07-29T23:33:03.977 回答
28

我刚刚浏览了所有这些,发现自定义验证器的 Rails 指南太硬编码......我在这里发布这个,即使它不完全是你问的,但 Q 标题非常适合(这就是我阅读这篇文章的原因对于我的问题)。

使用 i18n 消息进行自定义验证:

validate :something_custom?, if: :some_trigger_condition

def something_custom?
  if some_error_condition
    errors.add(:some_field_key, :some_custom_msg)
  end
end

# en.yml
activerecord: 
  errors:
    models:
      some_model:
        some_custom_msg: "This is i18n controlled. yay!"
于 2013-08-12T10:51:17.953 回答
3

对此的解决方案不需要定制;验证器format消息已经映射到:invalid符号。您只需在翻译中设置无效。

en:
  activerecord:
    errors:
      models:
        some_model:
          attributes:
            email:
              invalid: "FOO"

参考:http ://edgeguides.rubyonrails.org/i18n.html#error-message-interpolation

于 2015-08-26T15:09:49.620 回答
1

我会举一个完整的例子。

为了让你的模型更干净,你可以在一个全新的目录中创建一个自定义的验证辅助方法 - app/validations,它将由 Rails 自动加载。

我会打电话给我的文件time_validator.rb,位于app/validations/time_validator.rb

我的文件有以下代码,它验证用户输入的时间 - 它实际上是时间。

# frozen_string_literal: true

class TimeValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    Time.parse(value).strftime('%H:%M')
  rescue ArgumentError 
    record.errors.add(attribute, :invalid_time)
  end
end

您可以在此处阅读有关创建自定义 ActiveRecord 验证的更多信息

我们的主要关注点是record.errors线路。请注意,它是attribute而不是:attributeattribute模型中的列在哪里。

:invalid_time是从您的语言环境文件中检索翻译内容的键。就我而言,这是en文件:

en
  activerecord:
    errors:
      models:
        home:
          attributes:
            check_in_time:
              invalid_time: Please enter valid time
            check_out_time:
              invalid_time: Please enter valid time

然后在home模型中:

validates :check_in_time, time: { allow_blank: true }
validates :check_out_time, time: { allow_blank: true }

time自动映射到类TimeValidator,并运行其中的方法。

如果违反此规定,ActiveRecord 将在列名下方抛出错误。

希望这可以帮助!

于 2018-04-19T17:52:00.473 回答