2012-05-25 69 views
3

嗨我想驗證一個對象的列表中的對象。根實例來自JSON客戶端文件。對象驗證列表

我已經創建了這樣的事情

@Validateable 
class Book { 
    String title 
    List authors 
    static hasMany = [authors : Authors] 

    static constraints = { 
     authors(nullable:false,validator: { recipients -> 
      recipients.every { it.validate() } 
     })  
    } 
} 

@Validateable 
class Author { 
    String name 
    Integer property 

     static constraints = { 
     name(nullable:false)  
     property((nullable:false, blank: false))  
    } 
} 

而且我想這樣一來驗證它的控制器:

class BookController { 

def manageBook(Book book){ 
    if (book.validate()) { 
     //Do my stuff 
    }else{ 
     // return error 
    } 
} 
} 

但它不會在所有的工作,和我看到在控制檯中的這個錯誤:

| Error 2012-05-25 11:26:34,014 [http-bio-8080-exec-1] ERROR errors.GrailsExceptionResolver - MissingMethodException occurred when processing request: [POST] /BookApp/rest/bookApp/manageBook 
No signature of method: org.codehaus.groovy.grails.web.json.JSONObject.validate() is applicable for argument types:() values: [] 
Possible solutions: wait(), values(). Stacktrace follows: 
Message: No signature of method: org.codehaus.groovy.grails.web.json.JSONObject.validate() is applicable for argument types:() values: [] 
Possible solutions: wait(), values() 
    Line | Method 
->> 17 | doCall in dataskaterserver.DeviceSeenWifiData$__clinit__closure1_closure2_closure3 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
| 886 | runTask in java.util.concurrent.ThreadPoolExecutor$Worker 
| 908 | run . . in  '' 
^ 680 | run  in java.lang.Thread 

任何人都可以幫助我,請.. ..?

+0

您是否爲此映射配置了'parseRequest:true'? –

+0

是的,我已經定義了parseRequest:true。 我在URL映射已經這樣: \t \t 「/休息/ $控制器/ manageBook」(的parseRequest:真){ \t \t \t行動= [POST: 「manageBook」] \t \t} – elcadro

回答

1

我找到了解決辦法,這是文件上... http://grails.org/doc/latest/guide/validation.html#validationNonDomainAndCommandObjectClasses

註冊Validateable類 如果一個類不能與Validateable標記,它仍可以通過框架做出validateable。要做到這一點所需的步驟是通過在Config.groovy中@將值分配給所述grails.validateable.classes屬性來定義在類中的靜態屬性的約束(如上所述),然後講述類的框架:

grails.validateable.classes = [com.mycompany.myapp.User, com.mycompany.dto.Account]