gpt4 book ai didi

grails - 如何从Grails中的 Controller 声明inList约束?

转载 作者:行者123 更新时间:2023-12-02 14:02:56 26 4
gpt4 key购买 nike

谁能告诉我如何在Grails Controller 中声明inList约束?

假设我有这个类:

class A {
List hello
}

如何从 Controller 中为 inList hello添加 List约束?

最佳答案

定义一个约束,其中List属性的值已针对列表列表进行了验证?听起来怪怪的。但是你可以做到的。与这个类:

class A {
List hello
static constraint = {
hello inList:[['abc','def','ghi'],[1,2,3],['a','b']]
}
}

您可以在 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

但是,如果 A是其实例保留在传统数据库中的域类,则会删除 hello属性,因此您需要使用
static hasMany = [hello: SomeClass]

代替。

关于grails - 如何从Grails中的 Controller 声明inList约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14272680/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com