Module: ActiveModel::Validations::ClassMethods

Defined in:
lib/password_strength/active_model.rb

Instance Method Summary collapse

Instance Method Details

#validates_strength_of(*attr_names) ⇒ Object

Validates that the specified attributes are not weak (according to several rules).

class Person < ActiveRecord::Base
  validates_strength_of :password
end

The default options are :level => :good, :with => :username.

If you want to compare your password against other field, you have to set the :with option.

validates_strength_of :password, :with => :email

The available levels are: :weak, :good and :strong

You can also provide a custom class/module that will test that password.

validates_strength_of :password, :using => CustomPasswordTester

Your CustomPasswordTester class should override the default implementation. In practice, you’re going to override only the test method that must call one of the following methods: invalid!, weak!, good! or strong!.

class CustomPasswordTester < PasswordStrength::Base
  def test
    if password != "mypass"
      invalid!
    else
      strong!
    end
  end
end

The tester above will accept only mypass as password.

PasswordStrength implements two validators: PasswordStrength::Base and PasswordStrength::Validators::Windows2008.



82
83
84
# File 'lib/password_strength/active_model.rb', line 82

def validates_strength_of(*attr_names)
  validates_with StrengthValidator, _merge_attributes(attr_names)
end