开发者

Authlogic, can't update user because of validation

开发者 https://www.devze.com 2023-02-07 22:47 出处:网络
I\'ve got authlogic setup as such acts_as_authentic do |config| config.login_field = \'email\' config.merge_validates_length_of_email_field_options :in => 5..50

I've got authlogic setup as such

acts_as_authentic do |config|
  config.login_field = 'email'
  config.merge_validates_length_of_email_field_options :in => 5..50
  config.validates_length_of_password_field_options = {:on => :update, :minimum => 4 }
  config.validates_length_of_password_confirmation_field_options = {:on => :update, :minimum => 4}
end

I can't update user attributes because the password and password_confirmation try to get validated on开发者_如何学C save and they are nil. Is there any way to turn validations off temporarily, or a better solution?


Here's how it works for me:

Add a condition to your validate statements like this:

config.validates_length_of_password_field_options = {:on => :update, :minimum => 4, :if => :should_validate? }

Then add a custom should_validate? function to your user model. For example you could do

attr_accessor :updating_password    

def should_validate?
  updating_password or new_record?
end

This way you can explicitly set user.updating_password = true in your controller anytime you want the password to be validated and leave it as it is if you don't want any validation.

(This is my first answer, so I hope it's helpful for you. Otherwise don't hesitate to correct me.)

0

精彩评论

暂无评论...
验证码 换一张
取 消