Pietro
Pietro

Reputation: 1836

Grails constraint: unique between attributes values

I'm trying to add a constraint to check that two attributes have different values.

Here is my case:

class Game {
  static belongsTo = [ Team ]

  Team teamHome
  Team teamAway
}

What I'm trying to do is somenthig like:

static constraints = {
  teamHome( notEqual: teamAway )
  teamAway( notEqual: teamHome )
}

How can I solve this?

Upvotes: 1

Views: 209

Answers (1)

Ian Roberts
Ian Roberts

Reputation: 122424

You can use a validator constraint:

static constraints = {
  teamHome validator: { val, obj ->
    val != obj.teamAway
  }
}

The val argument is the teamHome value, and obj is the object that is being validated, through which you can access the teamAway property.

Upvotes: 2

Related Questions