【问题标题】:Override validations of gem devise覆盖 gem 设计的验证
【发布时间】:2015-04-23 01:37:53
【问题描述】:

在我的模型用户中,我添加了以下验证问题是设计已经实现了自己的验证。如何覆盖 Devise 的验证

用户.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable, :lockable and :timeoutable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me
  validates_presence_of :email, :message=>"email no puede estar vacio"
  validates_presence_of :password, :message=>"password no puede estar vacio"
  validates_presence_of :password_confirmation,:message=>"validate confirmation no puede estar vacio"
  end

浏览器

Email can't be blank  #validation of devise
Email email no puede estar vacio #my own validation
Password can't be blank #validation of devise
Password password no puede estar vacio #my own validation
Password confirmation validate confirmation no puede estar vacio

我遇到的其他问题是 rails 显示自定义消息之前的属性名称

Email email no puede estar vacio #my own validation #wrong
email no puede estar vacio #well

【问题讨论】:

  • 如果您重写设计验证只是为了以西班牙语显示一条消息,我认为您做错了。您应该编写一个西班牙语言环境文件 es.yml 并翻译此文件中的设计错误消息 (github.com/plataformatec/devise/wiki/I18n)。使用 I18n.locale = :es 将语言环境设置为西班牙语

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 devise


【解决方案1】:

您正在使用:validatable 设计模块...

您可以在confing/initializers/devise.rb中自定义以下几行

# ==> Configuration for :validatable
# Range for password length.
config.password_length = 8..128

# Email regex used to validate email formats. It simply asserts that
# one (and only one) @ exists in the given string. This is mainly
# to give user feedback and not to assert the e-mail validity.
# config.email_regexp = /\A[^@]+@[^@]+\z/

但如果您试图覆盖错误消息(并使用翻译前提到的字段名称解决问题), 你最好删除你的验证(有利于设计)

换句话说,您必须删除这些行

validates_presence_of :email, :message=>"email no puede estar vacio"
validates_presence_of :password, :message=>"password no puede estar vacio"
validates_presence_of :password_confirmation,:message=>"validate confirmation no puede estar vacio"

您可以使用Rails I18n API 覆盖验证消息 特别是part 4

简而言之...您必须在 config/locales/es.rb 中提供您首选语言 (:es) 的语言环境文件 提供那些特定的翻译

# please correct me if i'm wrong.
es:
  activerecord:
    errors:
      messages:
        record_invalid:
          email: email no puede estar vacio
          password: password no puede estar vacio
          password_confirmation: validate confirmation no puede estar vacio

但强烈建议将验证失败的字段放在那里。

注意: 如果您正在寻找只是为设计错误消息提供另一个语言环境(比如:es 语言环境)...

那么最好将config/locales/devise.en.yml 复制到config/locales/devise.es.yml 并进行相应的编辑... 或使用设计 wiki (github.com/plataformatec/devise/wiki/I18n)

【讨论】:

  • 感谢 artmees,如果我有两个文件 .yml“devise.es.yml”和“es.yml”会发生什么应用程序如何知道要使用哪个文件
  • 看看here..没问题..但我不知道如果你在同一个文件或多个文件中多次提供相同的密钥会发生什么
  • 最好只使用 devise.es.yml 覆盖设计字符串...但是您的语言环境 (es.yml) 会覆盖您的语言环境... 请花一些时间阅读文档。 ..要知道如何自定义和遵守约定...
猜你喜欢
  • 2012-03-15
  • 1970-01-01
  • 1970-01-01
  • 2011-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-30
  • 1970-01-01
相关资源
最近更新 更多