Is there a way to write a custom validator that will perform different validations according to field values?
For example
class myModel{
A a;
B b;
String prop
static belongsTo:[m:myModel]
constraints{
prop(validator:{
val,obj->
if (obj.a== null){
unique:[b,prop]
}
else{
unique:[a,b,prop]
}
})
}
}
I'm quite confused about this.
Thanks in advance
While not the most elegant solution, this should work:
static constraints = {
prop(validator: { val, obj ->
if(obj.a == null) {
return !myModel.findWhere(b: obj.b, prop: val)
} else {
return !myModel.findWhere(a: obj.a, b: obj.b, prop: val)
}
})
}
I don't believe there's a way to conditionally validate uniqueness based on property values without manually performing the query.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.