简体   繁体   中英

Validate custom constraints with JQuery Validation UI Plugin

I'm using grails JQuery Validation UI Plugin for grails 2.1.0.

I nearly have my form valadting client side based on my command object constraints. There is one problem, I have a custom constraint in my command object:

startTime(nullable: false, validator: {startTime, deal ->
    if ((!DateUtils.isSameDay(startTime, new Date()) && startTime.before(new Date()))) //Start date is today or future but not before end date
    {
        return "pastDate"
    }
    if (deal.endTime && startTime.after(deal.endTime)) {
        return "before.endTime"
    }
})


This results in the following rendered jQuery validation code generated with page markup:

startTime: {
    date: true,
    required: true,
    validator: {
    url: '/appContextRoot/JQueryRemoteValidator/validate',
    type: 'post',
    data: {
        validatableClass: 'myapp.command.tester.testerDealCommand',
        property: 'startTime'
    }
    }
}

The custum constrains logic withing "validator:" does nothing currently.

What is the best approach to get this custom validator working?
1: Some form of Ajax call?
2: Use Custom Constraits plugin and add js code to grails-validation-methods.js?
3: Some other way?

I'm unsure of using option 2....is there some way to extend the plugin?

I do not want to have to commit and maintain a seperate version of the plugin in our source code repository.

If I understood correctly the plugin, there's no need to add your custom validations directly to grails-validation-methods.js

If you load your script after, you can just follow his convention:

jQuery.validator.addMethod("myCustomValidation", function(value, element, params) {
    //perform your validation here
}, "Custom validation fail.");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM