I personally dislike validates_confirmation_of. Why? Because when I UPDATE a model it triggers, even when I don't want it to. However, setting it to allow nil values doesn't fix it -- it makes it so the value can be left blank!
I fought with this for a while, and came up with this method. What I am doing is saying that I only want to validate SOMETIMES. Other times I do not. The times I do not are when it is not a new record and the password was not provided.
I'm not certain if this allows someone to set their password to the empty string or not...
Here's the (probably gross) hack I'm doing:
validates_presence_of :password, :if => Proc.new { |u| u.password_needed? } validates_presence_of :password_confirmation, :if => Proc.new { |u| u.password_needed? } validates_length_of :password, :within => 4..40, :if => Proc.new {
u> u.password_needed? }
validates_confirmation_of :password, :if => Proc.new { |u| u.password_needed? }
def password_needed? if new_record? return true end if !password || password.empty? return false end false end