Thomas,
The particular problem in this case is that you are referring to an instance property from the static constraints block. That won't work (for the same reasons it doesn't work in Java). However, the Groovy error message is a little mystic. We should definitely point out common pitfalls like this.
What you really want is a custom validator:
Code:
class Response {
Survey survey
Answer answer
Store store
static belongsTo = [answer:Answer, survey:Survey, store:Store]
static constraints = {
survey(blank:false)
store(blank:false)
answer(blank:false), validator: { val, obj -> val in obj.survey.answers} )
}
String toString(){
return answer.toString()
}
}
The second argument to the validator closure is the Response instance, which allows you to access its instance 'survey' property. 'val' is the value that is being saved for that property.
We definitely value such feedback because it helps us improve the feedback mechanisms in Grails. In fact, we should be able to improve the error message in the above example, so I'll raise an issue.