使用设计时注册时禁用密码确认 [英] disable password confirmation during registration when using devise
问题描述
感谢所有。 :)
我有点迟到这个聚会,但刚刚在我的头上碰到同样的问题最后一个小时左右,我以为我会分享我找到的最简单的解决方案:,您可以从注册表单中删除 password_confirmation
字段。 >这无法完全确认密码!
其原因在于 lib /devise/models/validatable.rb
在Devise源中:
模块设计
模块模型
模块可验证
def self.included(base)
base.class_eval do
#.... SNIP。 ..
validates_confirmation_of:password,:if => :要求输入密码?
end
end
#... SNIP ...
def password_required?
!坚持? || !password.nil? || !password_confirmation.nil?
end
end
end
end
注意如果 password_required?
返回 true
和 password_required?$仅触发验证如果
password_confirmation
字段为 nil $ c $,则c $ c>将返回
false
C>。
由于表单中存在 password_confirmation
字段,因此它将始终包含在参数哈希中,如一个空字符串,如果它留空,则验证被触发。但是,如果您从表单中删除输入,参数中的 password_confirmation
将为 nil
,因此验证不会被触发。
希望这是有用的!
谢谢,
Tim
I am using Devise for Rails. In the default registration process, Devise requires users to type the password twice for validation and authentication. How can I disable it?
Thanks all. :)
I'm a little late to this party, but having just been scratching my head over the same problem for the last hour or so, I thought I'd share the easiest solution I've found: you can simply remove the password_confirmation
field from the registration form, which disables the need to confirm the password entirely!
The reason for this lies in lib/devise/models/validatable.rb
in the Devise source:
module Devise
module Models
module Validatable
def self.included(base)
base.class_eval do
#....SNIP...
validates_confirmation_of :password, :if => :password_required?
end
end
#...SNIP...
def password_required?
!persisted? || !password.nil? || !password_confirmation.nil?
end
end
end
end
Note that the validation is only triggered if password_required?
returns true
, and password_required?
will return false
if the password_confirmation
field is nil
.
Because where the password_confirmation
field is present in the form, it will always be included in the parameters hash , as an empty string if it is left blank, the validation is triggered. However, if you remove the input from the form, the password_confirmation
in the params will be nil
, and therefore the validation will not be triggered.
Hope this is useful!
Thanks,
Tim
这篇关于使用设计时注册时禁用密码确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!