Grails 多域关联验证问题?
我需要验证 3 个域之间的保存操作,以下是关系:
用户 - JobProcess:一对多,JobProcess - Heatmap:一对多。
User { static hasMany = [ jobs : JobProcess ] ... }
JobProcess { static hasMany = [ heatmaps : Heatmap ] ... User script ... }
Heatmap { static belongsTo = JobProcess ... JobProcess job ... }
我使用异常来控制验证流程,这是我的验证类:
class ValidationException extends RuntimeException {
Object invalidObject
ValidationException(String message, Object invalidObject) {
super(message)
this.invalidObject = invalidObject
}
}
我还构建了一个服务类来抽象热图操作(并将参数数据绑定到对象):
def addJob(params) {
def user = User.findById(params.user_id)
if (user) {
def heatmap = new Heatmap(params)
def process = new JobProcess(params)
process.addToHeatmaps(heatmap)
user.addToJobs(process)
if (user.save()) {
return heatmap
} else {
throw new ValidationException("Invalid form", heatmap )
}
}
}
以及我的控制器:
try {
def heatmap= HeatmapService.addJob(params)
flash.message = "Running new process : $heatmap.job}"
redirect(uri:'/')
} catch (ValidationException che) {
flash.message = "Validation Failed ${che.message}"
render(view:'create', model:[heatmap:che.invalidObject])
}
我的第一个问题:我使用以下方法没有错误验证:user.save() - 为什么不执行验证?
我可以解决这个问题,通过以下方式购买更新 if (user.save()) : if (heatmap.validate() && user.save())。
有更好的方法继续吗?
我的第二个问题:我的重定向没有触发无效帖子异常。
我被重定向到我的创建视图,但我的所有字段都是空白(我丢失了所有字段信息)并且没有验证错误。
尽管如此,我正在使用模型:[heatmap:che.invalidObject]。
除了例外,我传回 invalidObject,因此我的创建视图应该显示确切的验证错误?怎么了 ?
谢谢
I need to validate save action between 3 domains, here is relationship :
User - JobProcess : one-to-many, JobProcess - Heatmap : one-to-many.
User { static hasMany = [ jobs : JobProcess ] ... }
JobProcess { static hasMany = [ heatmaps : Heatmap ] ... User script ... }
Heatmap { static belongsTo = JobProcess ... JobProcess job ... }
I'm using Exceptions to control validation flow, here is my validation class :
class ValidationException extends RuntimeException {
Object invalidObject
ValidationException(String message, Object invalidObject) {
super(message)
this.invalidObject = invalidObject
}
}
I also build a service class to abstract Heatmap operations (and binds params data to object) :
def addJob(params) {
def user = User.findById(params.user_id)
if (user) {
def heatmap = new Heatmap(params)
def process = new JobProcess(params)
process.addToHeatmaps(heatmap)
user.addToJobs(process)
if (user.save()) {
return heatmap
} else {
throw new ValidationException("Invalid form", heatmap )
}
}
}
And my controller :
try {
def heatmap= HeatmapService.addJob(params)
flash.message = "Running new process : $heatmap.job}"
redirect(uri:'/')
} catch (ValidationException che) {
flash.message = "Validation Failed ${che.message}"
render(view:'create', model:[heatmap:che.invalidObject])
}
My first issue : I have no error validation by using : user.save() - why no validation are performs?
I can fix this issue buy updating if (user.save()) by : if (heatmap.validate() && user.save()).
Is there a better way to proceed ?
My second issue : my redirect isn't triggering invalid-post exception.
I'm redirected to my create view, but all my fields are blank (I lost all fields information) and no validation errors.
Nevertheless, I'm using model:[heatmap:che.invalidObject].
With the exception, I'm passing back the invalidObject, so my create view should display the exact validation errors ? What's wrong ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现我的问题,这是由于我的 GSP 视图,我使用了错误的 bean:
所以,我更新了热图,现在我得到了错误验证:
谢谢 Victor。
I find my issue, it's due to my GSP view, I was using the wrong bean :
So, I updated to heatmap, and now I get my error validation :
Thanks Victor.