0
Hi和因爲現在感謝無法通過beforeInsert事件
改變域類布爾屬性在域類我想基於使用beforeInsert事件的條件來改變一個布爾屬性,但布爾屬性不受影響。這裏是域類:
class Pet {
String name
String type
Boolean status = true
static constraints = {
name blank:false
type inList:["Dog", "Cat"]
}
def beforeInsert() {
status = (type == "Dog") ? true : false
}
String toString() { name }
}
我嘗試創建一個BootStrap.groovy中一些測試數據
class BootStrap {
def init = { servletContext ->
def nami = new Pet(name:"nami", type:"Dog")
if (!nami.save()) {
nami.errors.allErrors.each { error ->
log.error "[$error.field: $error.defaultMessage]"
}
}
def hotch = new Pet(name:"hotch", type:"Cat")
if (!hotch.save()) {
hotch.errors.allErrors.each { error ->
log.error "[$error.field: $error.defaultMessage]"
}
}
}
}
後的Grails運行的應用程序,我得到在控制檯下面的錯誤信息在那裏我得到的消息,財產狀況不能爲空
| Error 2014-10-07 13:27:28,281 [localhost-startStop-1] ERROR conf.BootStrap - [status: Property [{0}] of class [{1}] cannot be null]
| Error 2014-10-07 13:27:28,314 [localhost-startStop-1] ERROR conf.BootStrap - [status: Property [{0}] of class [{1}] cannot be null]
我試了沒有博olean屬性和beforeInsert運行良好,我甚至創建了另一個項目來複制場景和相同的行爲。
我所缺少的,我使用的Grails 2.3.8
感謝
感謝您的答案Alidad,但即使我從beforeInsert返回true,我也會得到相同的錯誤消息。我將所有類型的屬性設置爲Dog in bootstrap中的對象,以便始終將狀態設置爲true,但不成功 – user615274 2014-10-08 15:37:01