문제

For example, I have a domain class called:

class Employee {
     boolean belongToDepartment
     Department department

     static constraints = {
          department ????
     }
}

I want to write a validator for department which is if the field belongToDepartment is true, department is not null, otherwise department can be null.

I am not sure whether this is make sense?

도움이 되었습니까?

해결책

You can use custom validator on department to check if the boolean flag on the domain object is true and the department value is null. In that case it is a constraint failure, you can return false or an error code depending your need.

static constraints = {
      department nullable: true, validator: {dep, obj ->
          return !(obj.belongToDepartment && !dep)
      }
 }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top