简体   繁体   English

Grails验证:formRemote后未出现renderErrors

[英]Grails Validation : renderErrors not picking up after formRemote

This is in the gsp 这是在gsp中

<g:if test="${hasError}">
    <div class="errors">
        <g:renderErrors bean="${eventInstance}" />
    </div>
</g:if>
<g:else >
    <div id="messageBox" class="message" style="display:none;">
        <g:message code="legalevent.save.success" args="[entityName]" default="Event saved successfully" />
    </div>
</g:else>
<g:formRemote name="eventForm" id="eventForm" url="[controller : 'search', action : 'saveLegalEvent']"  
                  update="eventFormDiv" action="${createLink(controller: 'search', action: 'saveLegalEvent')}" method="POST"
                  onSuccess="jQuery('#messageBox').show()">

I am rendering a page for update with this : 我正在渲染一个页面以进行更新:

def saveLegalEvent = {

    def paramsView = params
    def eventPattern = /(event\.).*/
    def event = LegalEvent.findByLevId(params["levId"])
    def corrTxt = params["corrTxt"] as CorrectionText
    if(corrTxt.getCorrId()){
        corrTxt = CorrectionText.findByCorrId(corrTxt.getCorrId())
    }
    event.setCorrTxt(corrTxt)
    event.properties = params["event"] 
    def dataList = []
    def hasError = false
    def validated = event.validate()
    validated &= event.validateHistoryParams()
    if(validated)
        event.save(flush:true)
    else 
        hasError = true
    def errorsView = event.errors
    render(view:'leform', model:[attributeDataInstanceList:event.tags, lecInstance:event.leCode, eventInstance:event, hasError: hasError])
}

validateHistoryParams validate some more params that are usually not needed in the domain class. validateHistoryParams会验证域类中通常不需要的更多参数。

def validateHistoryParams = { ->
    if(!changeRef || !changeRef.trim()) {
        this.errors.rejectValue('changeRef', 'event.changeRef.blank')
    }
    if(!corrTxt || !(corrTxt.corrTxt.trim() || corrTxt.corrId )) {
        this.errors.rejectValue('corrTxt', 'event.corrTxt.null')
    }

    !(this.hasErrors())
}

The problem with all this is that the errors are not rendered in the gsp. 所有这些的问题是错误未在gsp中呈现。 All other tags are rendered fine, when debugging I can see that the errors are actually in the error stack. 所有其他标签都呈现良好,调试时我可以看到错误实际上在错误堆栈中。 But in the end, the tag isn't rendering them. 但是最后,标签没有渲染它们。

As you can see, there is no redirection, so I can't understand why the errors would somehow be erased between the response creation and the rendering ... 如您所见,没有重定向,所以我不明白为什么在响应创建和呈现之间会以某种方式消除错误...

In your Groovy code, parameter returned is named hasError , and GSP checks for hasErrors . 在您的Groovy代码中,返回的参数名为hasError ,并且GSP检查hasErrors I'd recommend not to use extra variables, and just query the bean itself in GSP. 我建议不要使用额外的变量,而只是在GSP中查询bean本身。

I also believe that you need to have that errors div inside the formRemote element in order to re-render after form submission. 我也相信,你需要有formRemote元素的错误DIV为了表单提交后重新渲染。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM