Search code examples
ruby-on-railsclient-side-validation

Override client_side_validations behavior to validate hidden fields


I am using the client_side_validations gem with Rails 3.1.0. I have been trying to use it to validate a hidden field that is set through a javascript widget. From what I have read on the gem's github page, the gem deliberately does not validate hidden fields because the user could not correct these fields. I have unsuccessfully tried to override this behavior.

Is there a way to override this behavior for a particular field without modifying the client_side_validations code (other than the init stuff that gets generated when you install the gem)?.

The way I've done it now is by making a slight modification to the validateForm function in the gem. When it looks for the fields to validate it only grabs the elements that are visible:

form.find(':input:enabled:visible[data-validate]').each(function() 

So I changed that to this:

form.find(':input:enabled[data-validate]').each(function() 

This seems to work. Is there a better way?

I haven't been able to re-validate the hidden field when the user corrects their mistakes.


Solution

  • If you want to validate hidden fields that is how to do it