如何更改表单验证消息 [英] How to change validation messages on forms

查看:110
本文介绍了如何更改表单验证消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发将在西班牙的网站。所以,我会在该语言所需要的错误消息。

the website I'm developing will be in spanish. Therefore, I'll need the error messages in that language.

我为了完成这个创建名为'en.yml'配置目录下的文件。我增加了以下code。在它:

I created a file under Configuration directory called 'en.yml' in order to accomplish this. And I added the following code in it:

es:
  activerecord:
    errors:
        models:
          announcement:
            attributes:
             title:
              blank: "El título no puede estar vacío."

厄尔尼诺TITULO没有puede易达讯vacío的意思是标题不能为空。

"El título no puede estar vacío" means "The title cannot be blank".

当我去运行此code我看到类似以下的消息:

When I go and run this code I see a message like the following:

标题厄尔尼诺TITULO没有puede易达讯   vacío。

"Title El título no puede estar vacío."

其中标题是字段的名称。但我不希望它被显示出来。我只是想显示我创建的错误消息。

Where "Title" is the field's name. But I don't want it to be displayed. I just want to display the error message I created.

推荐答案

您需要的属性后,指定转换

You have to specify the translation after the attribute

es:
  activerecord:
    models:
      announcement: "Anuncio"
    attributes:
      announcement:
        title: "Título"   # <= here
    errors:
      models:
        announcement:
          attributes:
            title:
              blank: "no puede estar vacío."

请参阅 5.1的翻译ActiveRecord模型了解更多信息。

这篇关于如何更改表单验证消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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