xml - 在Grails中使用XML进行动态约束

标签 xml grails groovy

我正在尝试在运行时动态填充static constraint ={}。有没有办法做到这一点。示例代码:

正常陈述:

static constraint = {
    lastName(blank:false, maxSize: 100)
}

我正在尝试做的是:
static constraint = {

    call to an XMLSlurper that returns a HashMap of lastName as a key and (blank: false, maxSize: 100) as a value.  // This part works.

    have the HashMap executed as if it where hard coded information to validate the fields. //This part does not work.

}

我希望这足以解释我的问题。

最佳答案

这是可能的,但是您尝试执行此操作的方式却不可行。使用GrailsHibernateDomainClass加载GORM类的约束。加载该类时,将评估validateConstraints方法和静态属性约束。您可以查看GrailsDomainConfigurationUtil中的validateConstraints方法,以了解如何对其进行评估。

如果要从其他来源添加自己的约束,则需要自行修改域类。最好的方法是在插件中。首先阅读plugin documentation,您的入口点是doWithSpring方法:

def doWithSpring { -> 
  application.getArtefacts(DomainClassArtefactHandler.TYPE).each { domainClass ->
    def myConstraints = getConstraintsFromXml() /* Create a Map<ConstrainedProperty> from your XML */
    domainClass.constrainedProperties.putAll myConstraints
  }
}

关于xml - 在Grails中使用XML进行动态约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3181416/

相关文章:

android - 如何在嵌套 View 中水平划分 View

c# - XML反序列化得到空值

javascript - 我的Grails应用重复发出http获取相同图像的请求(一个js驱动的时钟)。我需要怎么做才能做到呢?

Grails[Groovy],如何获取一个类没有继承的所有方法的列表?

git - 从 Jenkins 管道捕获 shell 脚本输出

java - 使用 ZipOutputStream 创建的 .war 文件无法部署

xml - 通过忽略该属性的 namespace 的属性名称选择 xml 节点

spring - Grails 3.1.16 - 从预定服务调用 session 范围服务的方法

grails - 如何将域强制转换为Grails中的集合集

grails - 如何以通用方式在域类上调用GORM方法?