从目标确认邮件中删除字段名 [英] remove field name from object validation message

查看:120
本文介绍了从目标确认邮件中删除字段名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单中使用这个对象上一个简单的活动记录验证:

I've got a simple active record validation on an object using this within a form:

  form.error_messages({:message => '', :header_message => ''})

这反过来又输出像字段名我的自定义消息

This in turn outputs something like "FieldName My Custom message"

我需要做的是从错误信息删除字段名称,但离开我的自定义消息。

What i need to do is remove the field name from the error message but leave my custom message.

任何人都可以点我在正确的方向这一点。

Can anyone point me in the right direction for this.

推荐答案

有完全控制信息的一种方法是使用自定义的验证块模型。例如检查一个字段不为空,将是这样的:

One way to have complete control over the messages is to use a custom validate block in the model. e.g. to check that a field is not blank it would be like this:

class MyModel < ActiveRecord::Base
  validate do |model|
    model.errors.add_to_base("My Custom message") if user.field.blank?
  end
end

add_to_base 是用于补充说,不相关的特定的个人领域(例如,如果多个字段的组合是非法的)消息。这意味着CSS来突出你的无效字段不会得到补充。您可以工作,这个角落找寻由还加入了无消息的错误你的域,比如

add_to_base is intended for adding messages that aren't related to a particular individual field (e.g. if a combination of multiple fields is illegal). This means that the CSS to highlight your invalid field won't get added. You can work arround this by also adding a nil message to the errors for your field e.g.

model.errors.add(:field, nil)

另外,请定制ERR消息插件 - 该插件为您提供了无法选择的自定义验证错误信息prefixed与属性的名称。

Alternatively, check out custom-err-message plugin - this plugin gives you the option to not have your custom validation error message prefixed with the attribute name.

这篇关于从目标确认邮件中删除字段名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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