2017-06-12 44 views
1

我有一個案例,驗證是在域屬性上完成的,但不在關聯(hasMany)屬性上。Grails域已擁有多個驗證

是否有任何配置可以添加以啓用兩個屬性(域和hasMany)的驗證。

的Grails版本:3.1.14

Example: 

class Person { 
     String name; 
     static hasMany = [location: Location] 
     static constraints = { 
     name nullable: true 
     } 
} 

class Location { 
     String address 
     String city 
     State state 
     String zip 

     static constraints = { 
     address nullable: true 
     } 
} 
+0

什麼版本的Grails您使用的是?將這個包含在你的問題中會很有幫助。 –

+0

grails 3.1.14 帖子也被編輯 –

+0

https://schneide.wordpress.com/2010/09/20/gorm-gotchas-validation-and-hasmany/你需要類似這樣的信息 – Vahid

回答

1

根據文檔,你想驗證應具有一對多協會工作:http://docs.grails.org/3.1.14/ref/Domain%20Classes/validate.html

但在我測試的它不eather工作。

的其他解決方案與約束的工作:

static constraints = { 
    name nullable: true 
    location validator: {val, obj -> 
     val.every { it.validate() } ?: 'invalid' 
    } 
} 
+0

與此處描述的相同的建議: https://stackoverflow.com/questions/16181599/grails-validate-nested-command-object-not-working – gregorr

+0

非常感謝,我嘗試了一些接近你的建議,在持久性監聽器級調用「hasMany」驗證器。 –