开发者

Grails Validation : renderErrors not picking up after formRemote

开发者 https://www.devze.com 2023-04-13 05:56 出处:网络
This is in the gsp <g:if test=\"${hasError}\"> <div class=\"errors\"> <g:renderErrors bean=\"${eventInstance}\" />

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
   开发者_StackOverflow社区 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消