validates :confirmation not executing when using :if condition
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
Please see the wiki.
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?
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 %>
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.
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
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'):
- 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.