2013-01-11 28 views

回答

2

定義一個約束,其中List屬性的值根據列表列表進行了驗證?聽起來怪怪的。但你可以做到。有了這個類:

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

,你可以在你的控制器做到這一點:

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] 

改爲。

+0

我需要它做dynamically.I我取出由Web服務的數據,我必須將它添加在hello列表(其值我已取出) – bharathi

+0

不,你不能在類定義中做到這一點。您可以定義一個域類來維護有效選項,並根據它檢查輸入值。 – coderLMN