Ruby on Rails:如何在字段为空时定制MailForm的错误消息

时间:2021-07-28 07:34:46

At the moment, if I leave a field in the form blank, I get an error message that is written in the en.yml file, how can I overwrite this error message in the model?

此时,如果我在表单中留空一个字段,就会得到一个写在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

This is what I've tried for the name attribute but I'm still getting the error message which is written in the en.yml file. I can't change the error message from en.yml as it is for another part of my application.

这就是我对name属性所做的尝试,但是我仍然得到了写在en中的错误消息。yml文件。我无法从en中更改错误消息。因为这是我申请的另一部分。

  validates :name, presence: { message: "Can't be blank" }

Any ideas why this is not overwriting the message?

有什么想法可以解释为什么不覆盖信息吗?

3 个解决方案

#1


3  

You can customize the error message using i18n localization in the same way that you would for a regular ActiveRecord model. The only difference is that you use the mail_form top-level scope instead of active_record.

您可以使用i18n本地化自定义错误消息,其方式与常规ActiveRecord模型相同。惟一的区别是您使用的是mail_form*范围,而不是active_record。

# en.yml
mail_form:
   errors:
      models:
         contact:
            attributes:
               name: 
                  blank: "My custom message goes here"

sources:

来源:

#2


0  

You don't need to nest the message in an inner hash. The syntax for messages on validations is:

不需要将消息嵌套在内部散列中。验证消息的语法是:

attribute :name, validate: true

Not very elegant, but you can set the error message manually:

不是很优雅,但是您可以手动设置错误消息:

contact = Contact.new
contact.valid?  # => false
contact.errors[:name] = "Can't be blank"  # => Will add "Can't be blank to the list of errors associated with  name"

Or, if you want to replace the original error:

或者,如果你想替换原来的错误:

contact.errors.set(:name, "Can't be blank")

#3


0  

If the above answers are not working (try them out first, rails is good with error messages) you could use JQuery to validate the fields before submit, there is a plugin for that https://jqueryvalidation.org/

如果上面的答案不起作用(首先尝试一下,rails很好地使用错误消息),您可以使用JQuery在提交前验证字段,有一个插件用于https://jqueryvalid.org。

From there site

从那里的网站

    $("#myform").validate({
  submitHandler: function(form) {
    // some other code
    // maybe disabling submit button
    // then:
    $(form).submit();
  }
 });

#1


3  

You can customize the error message using i18n localization in the same way that you would for a regular ActiveRecord model. The only difference is that you use the mail_form top-level scope instead of active_record.

您可以使用i18n本地化自定义错误消息,其方式与常规ActiveRecord模型相同。惟一的区别是您使用的是mail_form*范围,而不是active_record。

# en.yml
mail_form:
   errors:
      models:
         contact:
            attributes:
               name: 
                  blank: "My custom message goes here"

sources:

来源:

#2


0  

You don't need to nest the message in an inner hash. The syntax for messages on validations is:

不需要将消息嵌套在内部散列中。验证消息的语法是:

attribute :name, validate: true

Not very elegant, but you can set the error message manually:

不是很优雅,但是您可以手动设置错误消息:

contact = Contact.new
contact.valid?  # => false
contact.errors[:name] = "Can't be blank"  # => Will add "Can't be blank to the list of errors associated with  name"

Or, if you want to replace the original error:

或者,如果你想替换原来的错误:

contact.errors.set(:name, "Can't be blank")

#3


0  

If the above answers are not working (try them out first, rails is good with error messages) you could use JQuery to validate the fields before submit, there is a plugin for that https://jqueryvalidation.org/

如果上面的答案不起作用(首先尝试一下,rails很好地使用错误消息),您可以使用JQuery在提交前验证字段,有一个插件用于https://jqueryvalid.org。

From there site

从那里的网站

    $("#myform").validate({
  submitHandler: function(form) {
    // some other code
    // maybe disabling submit button
    // then:
    $(form).submit();
  }
 });