Grails domain class constraints for relation between fields
Asked Answered
T

2

6

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?

Torn answered 8/1, 2010 at 13:28 Comment(0)
F
11

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
  })
 }
}
Flinty answered 8/1, 2010 at 14:27 Comment(0)
L
2

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. Validation

Lap answered 8/1, 2010 at 13:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.