validates_confirmation_of not working

Hello to everyone,

Well, very frustrated with this one because I am following the example straight from the documentation and it is not working.

First when I try:

validates_confirmation_of :password, :on => :password_update (password_update is a def in my User model which works fine without this validation in please)

I get the following error:

TypeError in UsersController#update_password nil is not a symbol

[Full trace at the end of this message.]

However, if I do only this:

validates_confirmation_of :password

It jumps over the validation because no matter what I put in the password and password_confirmation fields, changes are always saved.

Any advice, greatly appreciated.

Thank you,

GPB

You can't do that. :on must be one of :save, :create or :update

Fred

Hello again...

I will answer my first question.

I thought the :on option could take any def from the controller and apply the validation only when that action was called. But now I see it only takes :save, :create and :update.

However, even when putting :update as the :on option, it is still bypassing the validation even though the data in the fields are not equal.

And one correction in my previous post, password_update is a def in my controller, not in my model, like I stated previously.

Thanks again,

Hello again...

I will answer my first question.

I thought the :on option could take any def from the controller and apply the validation only when that action was called. But now I see it only takes :save, :create and :update.

It's not a controller thing (how would the model know the action that
was causing it to be saved), it's a model thing: is the instance
being saved for the first time or all the time

However, even when putting :update as the :on option, it is still bypassing the validation even though the data in the fields are not equal.

Can't guess without seeing at least some code.

Fred

Hi Fred,

I went ahead and coded the validation myself.

However, I think the problem has to do with this, used for the encryption of the password.

  def password=(value)     if !value.blank?       write_attribute("password", Digest::SHA1.hexdigest(value))     end   end

Because I got it to the point that the validation worked, but it always returned that the values didn't match.

When I took out the lines for encryption it worked perfectly. So it seems it looks to compare the password string encrypted with the password_confirmation string not encrypted.

Care to explain how to work around this?

Thank you,

Hi DHH, you are right, it was a type on my part when writing the post, anyway Fred already explained I can't do that for the :on condition.

Either way, thank you.

I think if you do the hashing of the password in a before_update you'll be ok.

Fred

Thank you Fred, that worked.