custom simple constraint grails

拥有回忆 提交于 2019-12-30 07:08:12

问题


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]
    }
}

Somebody familiar with this case?


回答1:


To create your own constraint:

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

Take a look at the grails implementation of the size constraint for an example.



来源:https://stackoverflow.com/questions/14733844/custom-simple-constraint-grails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!