验证失败时设计自定义消息 [英] Devise custom messages when validation fails

查看:118
本文介绍了验证失败时设计自定义消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中使用devise作为身份验证引擎。
当设计验证失败时,是否有任何方式使用自定义消息。
当密码为空白时,Devise为我提供以下消息:密码不能为空白,但我需要另一条消息。我如何做?

解决方案

ActiveRecord en.yml是我建议的答案,如果要更改验证消息设计



这是en.yml应该如何看起来像

  en:
activerecord:
错误:
模型:
用户:
属性:
电子邮件:
blank:请指定电子邮件ID
take:请使用不同的电子邮件ID
无效:请指定有效的电子邮件ID
密码:
blank:请指定密码
确认:密码不匹配
password_confirmation:
blank:请指定密码确认
first_name:
blank:请指定名字
last_name:
blank:请指定姓氏
pdf:
属性:
名称:
空白:请指定名称为PDF
采取:请使用不同的名称为PDF
附件:
blank :请上传一个PDF附件
data_element:
属性:
名称:
blank:请给元素一个所需的名称
take:已经创建的元素给定名称
颜色:
blank:请分配一个颜色到元素
模板:
属性:
名称:
blank:请指定名称
take:请使用不同的名称

我建议你定义这种方式而不是定制设计验证模块



因为如果你遵循上述方法,那么你可能会跳过验证一个地方或两个



为例我删除上面的设计验证模块,然后在
用户模型



中替换自己,然后所有的验证都可以工作,但是你会错过更改中的验证密码



尽管密码从未提供,导致您登录,但永远不会提供



保持循环



欢呼



感谢


I'm using devise as authentication engine in my app. Is there any way to use custom messages when devise validation fails. Devise provides me with the following message when the password is blank: Password can't be blank, but i need another message. How can i do it?

解决方案

ActiveRecord en.yml is the answer I would suggest if you want to change the Validation Message for Devise

Here is how the en.yml should look like

en:
  activerecord:
    errors:
      models:
        user:
          attributes:
            email:
              blank: "Please Specify an Email id"
              taken: "Please use a different Email id"
              invalid: "Please Specify a valid Email id"
            password:
              blank: "Please Specify a Password"
              confirmation: "Password does not match"
            password_confirmation:
              blank: "Please Specify a Password Confirmation"
            first_name:
              blank: "Please Specify First Name"
            last_name:
              blank: "Please Specify Last Name"
        pdf:
          attributes:
            name:
              blank: "Please Specify name to PDF"
              taken: "Please use different name for PDF"
            attachment:
              blank: "Please Upload a PDF Attachment"
        data_element:
          attributes:
            name:
              blank: "Please give Element a desired name"
              taken: "Already Created Element with given name"
            color:
              blank: "Please assign a color to Element"
        template:
          attributes:
            name:
              blank: "Please Specify a Name"
              taken: "Please use a different name"

I advice you to define this way instead of customizing devise validation module

Because if you follow the above approach, it would be possible that you would skip a validation a place or two

for Example I the remove the above devise validation module and then substitue your own in User Model

then all the validation would work for but you would miss the validation in Change Password

There by resulting your to login even though the password was never supplied and never given

Keep a loop of that too

Cheer

Regards

这篇关于验证失败时设计自定义消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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