leo*_*rer 2 grails command-objects
commandObject当我提交表单时,我正在尝试使用它来验证我的数据.我可以在中验证hasMany关系吗commandObject?我的cenario是这样的.
牵引简单的classeswhith有很多关系:
class Book{
String nameBook
}
class Author{
String nameAuthor
static hasMany = [books:Book]
}
Run Code Online (Sandbox Code Playgroud)
很简单commandObject,我想在提交表单时验证hasMany.
@grails.validation.Validateable
class MyValidateCommand{
String nameAuthor
static hasMany = [books:Book]
static constraints = {
nameAuthor nullable:false
books nullable:false
}
}
Run Code Online (Sandbox Code Playgroud)
ps:我知道这个commandObject是错误的,它不编译.但我可以这样做吗?
hasMany在GORM中用于域对象中的关联.在命令对象的情况下,为每个域提供不同的命令对象(例如:AuthorCommand和BookCommand)将是一种清晰的方法,命令对象将如下所示:
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.
}
}
Run Code Online (Sandbox Code Playgroud)