limcheekin / jquery-validation-ui

JQuery Validation UI Plugin - Client Side Validation without writing JavaScript

Home Page:

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

bwilson opened this issue · comments

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.

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

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

Thanks, that works!

ezoic increase your site revenue