Search code examples
grailsconstraintsgrails-domain-classgrails-controller

domain class constraint in entry creation


In my Grails project I need to add a particular constraint to an entry of my domain class object. Domain class is as follows:

class HealthServiceType {
   String healthService;
   static belongsTo = [doctor:Doctor]
   static constraints = {

   }

   static mapping = {

   }
}

I need that healthService is not empty and is unique for each Doctor; that is, I can have multiple "test" value for healthService, but each one need to have different Doctor value.

Is it possible to perform this constraint in domain class? Or do I need to implement some check in Controller?


Solution

  • That's quite simple to make a unique property. For example:

    static constraints = {
      healthService(unique: ['doctor'])
    }
    

    The above will ensure that the value of healthService is unique for each value of doctor within your domain class.