[英]Grails domain class constraints for relation between fields
I need to write Domain class constraint in Grails which says that one integer field must be greater or equal than the other. 我需要在Grails中编写Domain类约束,它表示一个整数字段必须大于或等于另一个整数字段。
When I write the code like this: 当我写这样的代码时:
class MyDomain {
String title
int valueMin = 1
int valueMax = 1
static constraints = {
valueMin(min:1)
valueMax(min:valueMin)
}
}
I'm getting error: 我收到错误:
Caused by: groovy.lang.MissingPropertyException: No such property: valueMin for class: MyDomain
Any idea, please? 好吗,拜托?
http://grails.org/doc/latest/ref/Constraints/validator.html http://grails.org/doc/latest/ref/Constraints/validator.html
This should more or less work (not tested) 这应该或多或少的工作(未测试)
class MyDomain {
String title
int valueMin = 1
int valueMax = 1
static constraints = {
valueMin(min:1)
valueMax(validator:{
value, reference ->
return value > reference.valueMin
})
}
}
This wont work, because the constraints are a static block of code which will only have access to static variables. 这不会起作用,因为约束是一个静态的代码块,只能访问静态变量。
So, you could write your own customized cosntraint if you want: take a look at this link: http://grails.org/doc/latest/guide/single.html#7 . 因此,如果需要,您可以编写自己的自定义cosntraint:查看此链接: http ://grails.org/doc/latest/guide/single.html#7。 Validation 验证
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.