5

I need to write Domain class constraint in Grails which says that one integer field must be greater or equal than the other.

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?

4

2 回答 2

10

http://grails.org/doc/latest/ref/Constraints/validator.html

这应该或多或少起作用(未经测试)

class MyDomain {

 String title
 int valueMin = 1
 int valueMax = 1

 static constraints = {
  valueMin(min:1)
  valueMax(validator:{
    value, reference ->
    return value > reference.valueMin
  })
 }
}
于 2010-01-08T14:27:25.400 回答
2

这不起作用,因为约束是一个静态代码块,只能访问静态变量。

因此,您可以根据需要编写自己的自定义 cosntraint:查看此链接: http ://grails.org/doc/latest/guide/single.html#7 。验证

于 2010-01-08T13:59:39.980 回答