I'm trying to allow users to sign up to my app with external services such as twitter etc. Consequently I don't need a password for the user model which Authlogic tries to validate. As a result I'm disabling the password validations like so:
acts_as_authentic
before_validation :update_authlogic_config
#In the case that the user has signed up with an omniauth service.
attr_accessor :needs_no_password
def update_authlogic_config
validate_password_f开发者_运维问答ield = !needs_no_password
end
This all works well enough however it also seems to disable the email / username validations which I want to keep.
As a result, I updated my method to make sure the email field validates like so:
def update_authlogic_config
validate_password_field = !needs_no_password
ignore_blank_passwords = needs_no_password
validate_email_field = true
end
Using this it drags back in the password validations giving me the following errors:
Password is too short (minimum is 4 characters)
Password confirmation is too short (minimum is 4 characters)
Any ideas?
What you are looking for is a way to simply ignore password validation. This is a process called gradual engagement and is most commonly used to create a User account but not require specific things until the user truly needs them for more advanced systems operations. What you want to do is modify the authlogic config in your model like this:
acts_as_authentic do |c|
c.merge_validates_confirmation_of_password_field_options({:unless => :networked?})
c.merge_validates_length_of_password_field_options({:unless => :networked?})
c.merge_validates_length_of_password_confirmation_field_options({:unless => :networked?})
end
def networked?
self.authentications.any? # or true/false boolean of some kind
end
I got this information from the following gist: http://gist.github.com/436707/
If you don't put the crypted_password field in the User model you won't get the Password module loaded and therefor the validations won't run.
You'll find this is the regular behaviour of each of the modules in Authlogic.
精彩评论