在我的模型用户中,我添加了以下验证问题是设计已经实现了自己的验证。如何覆盖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
答案 0 :(得分:0)
您正在使用: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
用简单的话来说......你必须在:es
中提供首选语言(config/locales/es.rb
)的语言环境文件
提供那些特定的翻译
# 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)