目前,如果我将表单中的某个字段留空,我会收到一条写入 en.yml 文件中的错误消息,如何在模型中覆盖此错误消息?
class Contact < MailForm::Base
attribute :name, :validate => true
attribute :email, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
attribute :message, :validate => true
attribute :nickname, :captcha => true
这是我对 name 属性所做的尝试,但我仍然收到 en.yml 文件中写入的错误消息。我无法更改 en.yml 中的错误消息,因为它是我应用程序的另一部分的错误消息。
validates :name, presence: { message: "Can't be blank" }
有什么想法为什么这不会覆盖消息吗?
最佳答案
您可以使用 i18n
本地化来自定义错误消息,就像使用常规 ActiveRecord
模型一样。唯一的区别是您使用 mail_form
顶级范围而不是 active_record
。
# en.yml
mail_form:
errors:
models:
contact:
attributes:
name:
blank: "My custom message goes here"
来源:
关于ruby-on-rails - Ruby on Rails : How to customise error message for MailForm when field is blank,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37194578/