0
votes

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.

1
Validations like this that handle dates I prefer do on the server side, because javascript date object is odd (problems with daylight saving time and depends on machine/browser). So for your case I go with ajax call if you really need to do this before post the data. - user800014
Please only use appropriate tags. [jquery-validation-engine] is not the plugin you've linked to. Edited. - Sparky
@SérgioMichels - The date constraint is just the most simple example I could provide, I actually have more custom constraints which contain more complex domain logic. - Thomas Buckley

1 Answers

1
votes

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.");