自定义简单约束grails

自定义简单约束grails

问题描述:

我想创建一个自定义简单约束(如显示和可编辑),我可以在我的域类中使用。是否可以扩展ConstrainedProperty类?

I would like to create a custom simple constraint (like display and editable) which I can use within my Domain class. Is it possible to extend ConstrainedProperty class?

class City {
    String title
    BigDecimal latitude
    BigDecimal longitude
    Country country

    static constraints = {
        title       ( blank: false, customConstraint: true )  // filter can be also be applied as attributes: [customConstraint: true]
    }
}

有人熟悉这种情况吗?

要创建自己的约束:

  • extend org.codehaus.groovy.grails.validation.AbstractConstraint
  • register the class as a constraint by calling org.codehaus.groovy.grails.validation.ConstrainedProperty.registerNewConstraint

查看 grails实现大小约束例如。