(快速參考)

驗證

目的

根據套用約束驗證網域類別 (請參閱 驗證)

說明

驗證方法會根據已定義的 約束 驗證網域類別。錯誤會儲存在網域類別實例的 錯誤 屬性中。

驗證方法接受一個選用的 清單 參數,其中包含要驗證的屬性名稱。當指定名稱 清單 時,只會驗證那些屬性。

範例

def b = new Book(title: "The Shining")
if (!b.validate()) {
    b.errors.allErrors.each {
        println it
    }
}
def a = new Album(artist: "Genesis", title: "Nursery Cryme", releaseDate: 1971)

// only validate title and releaseDate
if (!a.validate(["title", "releaseDate"])) {
    a.errors.allErrors.each {
        println it
    }
}

參數

  • deepValidate (選用) - 是否也應驗證網域實例的關聯,亦即是否驗證串聯。預設為 true;將其設定為 false 以停用串聯驗證。