简体   繁体   中英

Grails Validation : renderErrors not picking up after formRemote

This is in the 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.

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. 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 . I'd recommend not to use extra variables, and just query the bean itself in GSP.

I also believe that you need to have that errors div inside the formRemote element in order to re-render after form submission.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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