Devise: Disable password confirmation during sign-up

后端 未结 9 1069
走了就别回头了
走了就别回头了 2020-12-24 01:13

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?

9条回答
  •  一向
    一向 (楼主)
    2020-12-24 01:44

    Devise's default validations (lib/devise/models/validatable.rb):

    validates_confirmation_of :password, :if => :password_required?
    

    and method:

    def password_required?
      !persisted? || !password.nil? || !password_confirmation.nil?
    end
    

    We need override Devise default password validation. Put the following code at the end in order for it not to be overridden by any of Devise's own settings.

    validates_confirmation_of :password, if: :revalid
    def revalid
      false
    end
    

    And your model would look like this:

    class User < ActiveRecord::Base      
      devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable,
         :confirmable, :timeoutable, :validatable
    
      validates_confirmation_of :password, if: :revalid
    
      def revalid
        false
      end
    end
    

    Then remove the password_confirmation field from the registration form.

提交回复
热议问题