覆盖 gem 设计的验证
Override validations of gem devise
在我的模型用户中,我添加了以下验证问题是设计已经实现了自己的验证。如何覆盖 Devise
的验证
user.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
您正在使用 :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
并进行相应的编辑...
或使用设计维基 (github.com/plataformatec/devise/wiki/I18n)
在我的模型用户中,我添加了以下验证问题是设计已经实现了自己的验证。如何覆盖 Devise
的验证user.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
您正在使用 :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
并进行相应的编辑...
或使用设计维基 (github.com/plataformatec/devise/wiki/I18n)