2013-08-22 54 views
2

我试图在提交表单时使用commandObject来验证我的数据。我可以验证commandObject中的hasMany关系吗?我的纪念日是这样的。命令对象和hasmany

拖车简单classes蒙山的hasMany关系:

class Book{ 
    String nameBook 
} 

class Author{ 
    String nameAuthor 
    static hasMany = [books:Book]  
} 

简单commandObject与我想验证提交表单时的hasMany。

@grails.validation.Validateable 
class MyValidateCommand{ 

    String nameAuthor 
    static hasMany = [books:Book] 


    static constraints = { 
     nameAuthor nullable:false 
     books nullable:false 
    } 

} 

ps:我知道这个commandObject是错误的,它不会编译。但我可以做这样的事吗?

+0

一个答案添加 –

回答

7

hasMany在GORM中用于关联域对象。在命令对象的情况下,这将是一个清晰的方法来为每个域不同的命令对象(例如:AuthorCommandBookCommand)和命令对象将是这样的:

import org.apache.commons.collections.list.LazyList 
import org.apache.commons.collections.functors.InstantiateFactory 

//Dont need this annotation if command object 
//is in the same location as the controller 
//By default its validateable 
@grails.validation.Validateable 
class AuthorCommand{ 
    String nameAuthor 
    //static hasMany = [books:Book] 

    //Lazily initialized list for BookCommand 
    //which will be efficient based on the form submission. 
    List<BookCommand> books = 
      LazyList.decorate(new ArrayList(), 
           new InstantiateFactory(BookCommand.class)) 

    static constraints = { 
     nameAuthor nullable:false 
     books nullable:false 

     //Let BookCommand do its validation, 
     //although you can have a custom validator to do some 
     //validation here. 
    } 
} 
+1

无法将书单是简单的:'名单 elias

0

不知道为什么没有,你可以尝试像(正常休眠的hasMany声明)

class MyValidateCommand{ 

    String nameAuthor 
    Set<Book> books= new HashSet<Book>(); 


    static constraints = { 
     nameAuthor nullable:false 
     books nullable:false 
    } 

}