Search code examples
validationgrailsgrails-orm

GORM constraints: validator for boolean fields


I'm trying to set a simple restriction on the field of the domain object using the closure in constraints but it does not work.

For example, I have three fields:

boolean organization1 = false
boolean organization2 = false
boolean organization3 = false

organization3 field can be set only if organization1 field is set:

class Organization {
    boolean organization1 = false
    boolean organization2 = false
    boolean organization3 = false

    static constraints = {
        organization1()
        organization2()
        organization3(validator:{organization3, organization -> return organization.organization1 ? true : false })
    }
}

Controller actions and GSP- views I get by using scaffolding. That's what happens:

enter image description here enter image description here

How to properly set a restriction? I would be very grateful for the information. Thanks to all.


Solution

  • Not sure if I got it correctly, but I'd put the validator this way:

    static constraints = {
        organization3 validator:{ org3, org -> !org3 || org3 && org.organization1 } 
    }