Validation grails 2.4.2-域对象验证的时间点

Validation grails 2.4.2-域对象验证的时间点,validation,grails,Validation,Grails,我有一个域类,其中包含一些自定义验证器,如下所示: class Domain { String attribute1 OtherDomain attribute2 static constraints = { attribute2 nullable: true, validator: {OtherDomain od, Domain d -> if (od) { log.debug "enter

我有一个域类,其中包含一些自定义验证器,如下所示:

class Domain {
    String attribute1
    OtherDomain attribute2

    static constraints = {
        attribute2 nullable: true, validator: {OtherDomain od, Domain d ->
            if (od) {
                log.debug "entering validation"
                // certain validation here
            }
    }
}
对于更新,我在相应的
域控制器中有一个简单的操作:

@Transactional
def update(Domain domainInstance) {
    log.debug "entering update()"
    // rest of update
}
我想知道为什么在我的
debuglog
中,我会按以下顺序得到调试消息:

  • 进入验证
  • 进入update()
  • 问题是验证在此时失败(
    StackOverflowerError
    )。我知道此错误的原因,也知道如何避免此错误(在
    update
    action中执行此操作)。但是,我不知道为什么在程序进入
    update()
    操作之前会进行验证。我不知道如何在这一点上阻止验证

    您有什么建议吗?

    在“输入更新()”之前看到“输入验证”消息的原因是您声明了一个类型为
    域的命令对象。这意味着Grails将把任何请求参数绑定到
    domainInstance
    ,然后在执行操作之前对其调用
    validate()
    。这允许您编写如下代码:

    @Transactional
    def update(Domain domainInstance) {
        // at this point request params have been bound and validate() has 
        // been executed, so any validation errors will be available in
        // domainInstance.errors
    
        if (domainInstance.hasErrors() {
            // do something
        }
    
        log.debug "entering update()"
    }
    
    在“entering update()”之前看到“entering validation”消息的原因是您声明了类型为
    Domain
    的命令对象。这意味着Grails将把任何请求参数绑定到
    domainInstance
    ,然后在执行操作之前对其调用
    validate()
    。这允许您编写如下代码:

    @Transactional
    def update(Domain domainInstance) {
        // at this point request params have been bound and validate() has 
        // been executed, so any validation errors will be available in
        // domainInstance.errors
    
        if (domainInstance.hasErrors() {
            // do something
        }
    
        log.debug "entering update()"
    }
    

    谢谢你的回答和解释!当我重新阅读有关数据绑定的文档时,我也在这里找到了它:感谢您的回答和解释!当我重新阅读有关数据绑定的文档时,我也在这里找到了它: