0

Grails 2.1.1

I can't seem to get a command object to be injected with a service so that I can use custom validator. I've tried several things, including Grails command object data binding and what the 2.1.1 docs on custom validator suggest, I just can't figure this one out..

Relevant Code:

class RegistrationCommand {

    String username

    def registrationService
    static constraints = {

        username validator: { val, obj ->
            obj.registrationService.isUsernameUnique(val) }
    }
}

class RegistrationService {

    def isUsernameUnique(username){
        def user = new User(username:username)
        user.validate()
        if(user.errors.hasFieldErrors("username")){
            return false
        }else{
            return true
        }
    }
Community
  • 1
  • 1
jbowen7
  • 71
  • 1
  • 5
  • 1
    Do you get an error? If so, what is it? – doelleri Nov 05 '12 at 21:15
  • A nearly identical case works fine for me under 2.1.1. In my case, including the obj second argument and using it was the key, but you already have that. – Peter N. Steinmetz Nov 21 '12 at 02:01
  • Yes, I have @grails.validation.Validateable.. and the error I get is NullPointerException (Cannot invoke method isUsernameUnique() on null object) . – jbowen7 Mar 14 '13 at 18:59

1 Answers1

2

Resolved.. Issue was due to plugin. I'm using a plugin for client side jquery validation (jquery-validation-ui-1.4.2). The command object being created by the plugin's controller wasn't getting injected with the service. The issue was reported https://github.com/limcheekin/jquery-validation-ui/issues/17 . The fix does work but has not been pushed upstream yet.

jbowen7
  • 71
  • 1
  • 5