Can anyone show me how to declare an inList
constraint within a Grails controller?
Let’s say I have this class:
class A {
List hello
}
How can I add the inList
constraint for the hello
List
from within the controller?
Can anyone show me how to declare an inList
constraint within a Grails controller?
Let’s say I have this class:
class A {
List hello
}
How can I add the inList
constraint for the hello
List
from within the controller?
Define a constraint in which a List
property has values validated against a list of lists? Sounds weird. But you can do it. With this class:
class A {
List hello
static constraint = {
hello inList:[['abc','def','ghi'],[1,2,3],['a','b']]
}
}
you can do this in your controller:
def instance1 = new A(hello:['abc','def','ghi']).save() //valid
def instance2 = new A(hello:[1,2,3]).save() //valid
def instance3 = new A(hello:['a','b']).save() //valid
def instance4 = new A(hello:['a','b','c']).save() //invalid
def instance5 = new A(hello:[1,2]).save() //invalid
If A
is a domain class whose instances are persisted in a traditional database, however, the hello
property would be dropped, so you’d need to define it using
static hasMany = [hello: SomeClass]
instead.
You can write a custom validator to your fields that check if data are in List. You will have to implement the in list checking manually. You can find here the official documentation. There are some stackoverflow entries that can help you like
Grails: Custom validator based on a previous value of the field