使用设计时注册时禁用密码确认 [英] disable password confirmation during registration when using devise

查看:115
本文介绍了使用设计时注册时禁用密码确认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Devise for Rails。在默认注册过程中,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 将返回 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆