Question

I wanna use the <g:submitToRemote value="Submit" update="[success:'resultContent',failure:'errorPage']" action="submit" /> to enable update the resultContent div when the validation is passed, but update the errorPage div when the validation is not passed.

But I don't know how to let the grails know that the validation is not passed. This is the code I use

In the controller

def error=validate(params)
def user=User.get(1)
try{
    if(error){
        params.error=error

        throw new Exception("Validation Failure")

    }else{
        render(view:'result')
    }
}
catch(Exception e){
    render(view:'error')
}

In the gsp

...
<div id="errorPage"></div>
...
<div id="resultContent"></div>
...

What I want is to update the resultContent when all the values are valid, but to update the errorPage when there is some validation errors. The resultContent div is updated. But the errorPage is untouched. How can I let the grails know the errorPage div should be updated?? I guess I should make some changes in the controller to let the gsp page know that the validation is in a "failure" state. But I can't find an example online to do this. Please help!

Was it helpful?

Solution

If you are wanting to create a new User based on params you can do something like this:

def user = new User(params)
user.validate()

if ( user.hasErrors() ) {
    render template: 'error', model: [user: user], status: '403'
} else {
    render view: 'result'
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top