【问题标题】:Customize "Password confirmation doesn't match Password" in Rails在 Rails 中自定义“密码确认与密码不匹配”
【发布时间】:2013-10-09 16:17:00
【问题描述】:

有没有办法在 Rails 中为确认字段自定义消息?例如在设计中我必须输入密码和密码确认,错误消息是:

密码确认与密码不匹配

我可以更改活动记录区域设置消息(“不匹配”),但它会在该区域设置消息的开头和结尾输出密码确认和密码,所以我得到如下信息:

“密码确认必须与密码匹配”

有没有办法把它改成不同的字符串?

密码确认和密码必须匹配。

编辑

另一件事是拥有完全自定义的消息,例如:

“设置密码”和“确认密码”必须匹配。

【问题讨论】:

    标签: ruby-on-rails ruby validation devise


    【解决方案1】:

    那些错误消息属于activerecord

    只需创建一个具有该结构的新语言文件并替换您需要的内容。

    activerecord:
      errors:
        messages:
          confirmation: "and Password must match."
    

    您可以为模型或模型属性定义自己的错误。 :model、:attribute 和 :value 值始终可用于插值。

      For example,
       models:
           user:
             blank: "This is a custom blank message for %{model}: %{attribute}"
             attributes:
               login:
                 blank: "This is a custom blank message for User login"
    

    请根据您的要求在此链接上阅读更多内容 https://github.com/rails/rails/blob/master/activerecord/lib/active_record/locale/en.yml

    【讨论】:

    • 它有效。但我想要完全定制。查看我的编辑。
    • 问题是我要自定义的错误是虚拟的(password_confirmation)并且它在数据库中不存在。
    • @ИванБишевац 即使我将它作为设计中的虚拟属性,而不是在架构中,上述解决方案(结构中的通用 1,即 I18n.t('activerecord.errors.messages.confirmation') ),对我有用。
    【解决方案2】:

    ActiveRecord en.yml 是我建议您更改设计验证消息的答案

    这里 en.yml 应该是什么样子

    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"
    

    我建议您以这种方式定义,而不是自定义设计验证模块

    因为您遵循上述方法,所以您可能会跳过一两个地方的验证

    例如我删除上面的设计验证模块,然后在用户模型中替换你自己的

    那么所有验证都可以使用,但您会错过更改密码中的验证

    即使您从未提供过密码也从未提供过密码,但您仍然可以登录

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-04
      • 2018-05-06
      • 2021-07-16
      • 2013-11-25
      • 2014-05-08
      • 2014-10-18
      • 1970-01-01
      相关资源
      最近更新 更多