limcheekin / jquery-validation-ui

JQuery Validation UI Plugin - Client Side Validation without writing JavaScript
http://limcheekin.github.io/jquery-validation-ui/
Apache License 2.0
20 stars 18 forks source link

Custom validator appears in rules instead of $.validator.addMethod #36

Closed bwilson closed 10 years ago

bwilson commented 10 years ago

I created a custom validator by adding a Javascript function to CustomConstraintsMap in Config.groovy. The result is that renderValidationScript writes the function directly where the method name should go in the rules map. This produces a JavaScript error: "t.validator.methods[a] is undefined."

According to the jQuery Validation Plugin docs, the custom code should go in $.validator.addMethod.

I'm not sure if I'm doing something wrong or perhaps the approach has changed.

teddyyueh commented 10 years ago

In your loaded Javascript, you must add a method for your validator. Here's an example:

jQuery.validator.addMethod("noUnderscore", function(value, element) { return this.optional(element) || value.indexOf('_') < 0; }, "Cannot contain underscores");

What I did was include my own additional-methods.js file (as validate-additional-methods.js) in a module that I also include along with 'jquery-validation-ui' or 'jquery-validation-ui-qtip'.

In config.groovy:

CustomConstraintsMap = [ phone:'true', // International phone number validation phoneUS:'true', alphanumeric:'true', letterswithbasicpunc:'true', lettersonly:'true', numbersonly:'true', noUnderscore:'true' ]

Also, if you need server-side validation as well, you can add it in doWithSpring with ConstrainedProperty.registerNewConstraint.

bwilson commented 10 years ago

Thanks, that works!