Grails 命令对象和hasmany
我试图在提交表单时使用Grails 命令对象和hasmany,grails,command-objects,Grails,Command Objects,我试图在提交表单时使用commandObject验证我的数据。我可以验证commandObject中的hasMany关系吗。我的塞纳里奥是这样的 两个简单的类有很多关系: class Book{ String nameBook } class Author{ String nameAuthor static hasMany = [books:Book] } 简单的commandObject,在提交表单时有许多我想要验证的内容 @grails.validatio
commandObject
验证我的数据。我可以验证commandObject
中的hasMany关系吗。我的塞纳里奥是这样的
两个简单的类
有很多关系:
class Book{
String nameBook
}
class Author{
String nameAuthor
static hasMany = [books:Book]
}
简单的commandObject
,在提交表单时有许多我想要验证的内容
@grails.validation.Validateable
class MyValidateCommand{
String nameAuthor
static hasMany = [books:Book]
static constraints = {
nameAuthor nullable:false
books nullable:false
}
}
Ps:我知道这个commandObject是错误的,它不能编译。但是我可以这样做吗?
在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.
}
}
import org.apache.commons.collections.list.LazyList
导入org.apache.commons.collections.functors.InstanceFactory
//如果命令对象
//与控制器位于同一位置
//默认情况下,它是可验证的
@grails.validation.validatable
类AuthorCommand{
字符串名作者
//静态hasMany=[书籍:书籍]
//BookCommand的延迟初始化列表
//根据提交的表格,这将是有效的。
书目=
装饰(新的ArrayList(),
新实例化属性(BookCommand.class))
静态约束={
nameAuthor可为空:false
可作废的书籍:错误
//让BookCommand进行验证,
//尽管您可以使用自定义验证器来执行某些操作
//在这里验证。
}
}
不确定为什么您不能像这样尝试(正常的hibernate有很多声明)
类MyValidateCommand{
字符串名作者
Set books=new HashSet();
静态约束={
nameAuthor可为空:false
可作废的书籍:错误
}
}
图书列表不能简单地为:列表吗
class MyValidateCommand{
String nameAuthor
Set<Book> books= new HashSet<Book>();
static constraints = {
nameAuthor nullable:false
books nullable:false
}
}