2012-11-05 53 views
0

的Grails 2.1.1的Grails 2.1.1命令對象服務注射自定義的驗證

我似乎無法得到一個命令對象與服務注入,這樣我可以使用自定義的驗證。我已經試過幾件事,包括 Grails command object data binding和 什麼自定義驗證2.1.1文檔建議,我實在不明白這一個..

相關代碼:

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 
     } 
    } 
+1

你有錯誤?如果是這樣,那是什麼? – doelleri

+0

你有'@ Validateable'嗎? –

+0

幾乎相同的情況在2.1.1下適合我。在我的情況下,包括obj第二個參數並使用它是關鍵,但你已經有了。 –

回答