Grails Domain Class Limitations for Field Relations

I need to write a class class constraint in Grails that says that one integer field must be greater than or equal to another.

When I write the code as follows:

class MyDomain {

 String title
 int valueMin = 1
 int valueMax = 1

 static constraints = {
  valueMin(min:1)
  valueMax(min:valueMin)
 }
}

I get an error message:

Caused by: groovy.lang.MissingPropertyException: No such property: valueMin for class: MyDomain

Any idea please?

+5
source share
2 answers

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

This should more or less work (not verified)

class MyDomain {

 String title
 int valueMin = 1
 int valueMax = 1

 static constraints = {
  valueMin(min:1)
  valueMax(validator:{
    value, reference ->
    return value > reference.valueMin
  })
 }
}
+8
source

This does not work because constraints are a static block of code that will only access static variables.

, cosntraint, : : http://grails.org/doc/latest/guide/single.html#7.

+2

All Articles