在验证错误消息中不包含字段名

时间:2021-12-03 23:26:23

If I have the following validation:

如果我有以下确认:

validates_inclusion_of :dob, :in => Date.new(1900)..Time.now.years_ago(18).to_date, :message => "You must be 18 or older to register"

The actual message on the site that shows up is:

网站上显示的实际信息是:

"Dob You must be 18 or older to register"

"你必须年满18岁才能注册"

Is there any way not to include the column name at the beginning of the message?

是否有办法不将列名包含在消息的开头?

Thank you

谢谢你!

Tom

汤姆

2 个解决方案

#1


3  

Yes. I use the custom-err-msg plugin. It's pretty easy to get up and running, just read the README.

是的。我使用定制的-msg插件。站起来跑步很容易,只要看看自述就行了。

#2


0  

Full disclosure: I developed the gem.

充分披露:我开发了宝石。

I feel existing solutions are hacks, and I developed this gem to avoid such hacks.

我觉得现有的解决方案都是黑客,我开发了这个gem来避免这种攻击。

https://github.com/lulalala/adequate_errors

https://github.com/lulalala/adequate_errors

After installing the gem, set the error message in your locale yaml file like this:

安装gem之后,在语言环境yaml文件中设置错误消息如下:

en:
  activemodel
    adequate_errors:
      models:
        user:
          attributes:
            dob:
              inclusion: "You must be 18 or older to register"

And then accessing model.errors.adequate.messages will have the message without attribute prefix.

然后访问model.errors.adequate。消息将具有没有属性前缀的消息。

#1


3  

Yes. I use the custom-err-msg plugin. It's pretty easy to get up and running, just read the README.

是的。我使用定制的-msg插件。站起来跑步很容易,只要看看自述就行了。

#2


0  

Full disclosure: I developed the gem.

充分披露:我开发了宝石。

I feel existing solutions are hacks, and I developed this gem to avoid such hacks.

我觉得现有的解决方案都是黑客,我开发了这个gem来避免这种攻击。

https://github.com/lulalala/adequate_errors

https://github.com/lulalala/adequate_errors

After installing the gem, set the error message in your locale yaml file like this:

安装gem之后,在语言环境yaml文件中设置错误消息如下:

en:
  activemodel
    adequate_errors:
      models:
        user:
          attributes:
            dob:
              inclusion: "You must be 18 or older to register"

And then accessing model.errors.adequate.messages will have the message without attribute prefix.

然后访问model.errors.adequate。消息将具有没有属性前缀的消息。