(快速參考)

約束

目的

允許定義宣告式驗證約束。請參閱使用者指南中的驗證

範例

class Book {

    String title
    Author author

    static constraints = {
        title blank: false, size: 5..150
        author nullable: true
    }
}

說明

約束使用宣告式約束 DSL 定義,如使用者指南中的驗證部分所述。評估後,驗證可透過使用驗證方法套用

def b = new Book()
assert !b.validate()

靜態 constrainedProperties 屬性是 Map,其中 Map 中的鍵是屬性名稱,而與鍵關聯的值是ConstrainedProperty的執行個體

def constraintsMap = Book.constrainedProperties
for(entry in constraintsMap) {
    // propertyName is a String
    def propertyName = entry.key

    // constrainedProperty is a ConstrainedProperty
    def constrainedProperty = entry.value

    // ...
}