I'd like to be able to have a constraint implemented as optional based on another field of the domain class.
Such that, if importMode
is true, the company field is no longer required, but if import mode is false the default functionality provided by company (blank:false)
is invoked.
class MyClass {
boolean importMode
String company
static constraints = {
company(validator: { val, obj ->
if(obj.importMode) {
// return default blank:false functionality
}
return true // else pass
}
}
}
Is it possible in Grails to call the built in constraints from a custom constraint closure like this?
You have to define a custom constraint, not use the default nullable or blank constraint
class MyClass {
boolean importMode
String company
static constraints = {
company(validator: { val, obj ->
if(!obj.importMode && !val) {
return['myClass.company.required']
}
})
}
}