client_side_validations icon indicating copy to clipboard operation
client_side_validations copied to clipboard

validates :confirmation not executing when using :if condition

Open geek0x23 opened this issue 15 years ago • 6 comments

In my user model, I have a simple setup:

attr_accessor :password, :updating_password
attr_accessible :password, :password_confirmation, :updating_password

validates :password, :presence { :if => :should_validate_password? },
                     :confirmation { :if => :should_validate_password? },
                     :length { :minimum => 4, :if => :should_validate_password? }

def should_validate_password?
  updating_password || new_record?
end

As you can see, I only want to validate the password when the user is actually updating it (which is handled in the controller), or it's a new record. The presence and length validations work perfectly on the client side without any issues, however the confirmation validation does not. If I remove the condition, then the confirmation validation works client-side like it should.

The validation does happen on post back, but I'd prefer it to happen client side. I believe this is a bug, but I have no idea where to look to fix it, otherwise I'd do it myself and submit a pull.

Thanks,

Scott

geek0x23 avatar Jun 25 '11 07:06 geek0x23

Please see the wiki.

bcardarella avatar Jun 25 '11 15:06 bcardarella

I'm sorry. I've read over the wiki and I don't see anything related to this. Why would it work presence and length validators but not with confirmation?

geek0x23 avatar Jun 25 '11 17:06 geek0x23

I'm sorry, I thought you were using Devise. This is a common issue with Devise.

By default, the gem will ignore conditional validators. So you'll need to force them:

   <%= f.text_field :password, :validate => true %>

bcardarella avatar Jun 25 '11 17:06 bcardarella

Ah, alright. Thanks. Yeah I'm not a fan of devise so I'm using my own auth scheme :)

Thanks for the heads up! This works perfectly.

geek0x23 avatar Jun 25 '11 19:06 geek0x23

Glad it works. :) On Jun 25, 2011 3:32 PM, "swaj" < [email protected]> wrote:

Ah, alright. Thanks. Yeah I'm not a fan of devise so I'm using my own auth scheme :)

Thanks for the heads up! This works perfectly.

Reply to this email directly or view it on GitHub:

https://github.com/bcardarella/client_side_validations/issues/116#issuecomment-1438928

bcardarella avatar Jun 25 '11 19:06 bcardarella

I was stuck on this for a while too. Couldn't find anything in the wiki about this, perhaps there could be a 'Conditional Validations' link?

I also assumed from your Project goals that this was default behaviour (and didn't need to be 'switched on'):

  1. In the cases where a server-side validation rule would not work on the client (i.e. conditional callbacks like :if, :unless) then do not attempt client side validations. Fall back to the server side validation.

Your gem is absolutely amazing, by the way. Thanks a heap.

robertdapice avatar Nov 29 '11 06:11 robertdapice