Rails 4:从自定义验证器的错误消息中删除属性名称 [英] Rails 4: remove attribute name from error message in custom validator

查看:46
本文介绍了Rails 4:从自定义验证器的错误消息中删除属性名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Rails 4 应用程序中,我在我的 Post 模型上实现了一个名为 LinkValidator 的自定义验证器:

In my Rails 4 app, I implemented a custom validator named LinkValidator on my Post model:

class LinkValidator < ActiveModel::Validator

  def validate(record)
    if record.format == "Link"
      if extract_link(record.copy).blank?
        record.errors[:copy] << 'Please make sure the copy of this post includes a link.'
      end
    end
  end

end

一切正常,除了当前显示的消息是:

Everything works fine, except that currently, the message displayed is:

1 error prohibited this post from being saved:
Copy Please make sure the copy of this post includes a link.

如何去掉上面消息中的copy"二字?

How can remove the word "copy" from the above message?

我试过 record.errors <<'...' 而不是 record.errors[:copy] <<'...' 在验证器中,但随后验证不再有效.

I tried record.errors << '...' instead of record.errors[:copy] << '...' in the validator, but then the validation no longer works.

有什么想法吗?

推荐答案

不幸的是,目前 full_messages 的错误格式是用一个 I18n 键控制 errors.format,因此对其的任何更改都将产生全球性后果.

Unfortunately currently the format of full_messages for errors is controlled with a single I18n key errors.format, hence any change to it will have a global consequences.

常见的选项是将错误附加到基础而不是属性,因为基础错误的完整消息不包括属性人名.我个人不喜欢这个解决方案的原因有很多,主要是如果验证错误是由字段 A 引起的,它应该附加到字段 A.这才有意义.期间.

Common option is to attach error to the base rather than to the attribute, as full messages for base errors do not include attribute human name. Personally I don't like this solution for number of reason, main being that if the validation error is caused by a field A, it should be attach to field A. It just makes sense. Period.

虽然这个问题没有很好的解决方法.肮脏的解决方案是使用猴子补丁.将此代码放在 config/initializers 文件夹中的新文件中:

There is no good fix for this problem though. Dirty solution is to use monkey patching. Place this code in a new file in your config/initializers folder:

module ActiveModel
  class Errors
    def full_message(attribute, message)
      return message if attribute == :base
      attr_name = attribute.to_s.tr('.', '_').humanize
      attr_name = @base.class.human_attribute_name(attribute, :default => attr_name)
      klass = @base.class
      I18n.t(:"#{klass.i18n_scope}.error_format.#{klass.model_name.i18n_key}.#{attribute}", {
                                 :default   => [:"errors.format", "%{attribute} %{message}"],
                                 :attribute => attr_name,
                                 :message   => message
                             })
    end
  end
end

这保留了 full_messages 的行为(根据 Rails 4.0),但是它允许您覆盖特定模型属性的 full_message 格式.因此,您可以在翻译中的某处添加这一点:

This preserves the behaviour of full_messages (as per rails 4.0), however it allows you to override the format of full_message for particular model attribute. So you can just add this bit somewhere in your translations:

activerecord:
  error_format:
    post:
      copy: "%{message}"

老实说,我不喜欢没有干净的方法可以做到这一点,这可能值得一个新的宝石.

I honestly dislike the fact that there is no clean way to do this, that probably deserves a new gem.

这篇关于Rails 4:从自定义验证器的错误消息中删除属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆