class Person {
String name
Integer age
String street
String email
}
class PersonController {
def save = {
def personInstance = new Person(params)
personInstance.save(flush:true)
}
}
main.gsp
<g:form controller="person" action="save">
<g:textField name="name" />
<g:textField name="age" />
<g:textField name="street" />
<g:textField name="email" />
<g:submitButton name="save" value="Save" />
</g:form>
What modifications do i have to make in both the controller and the gsp page to render the errors to the gsp page?
Change the controller action to
def save = {
def personInstance = new Person(params)
if (!personInstance.save(flush: true)) {
render view: 'create', model: [personInstance: personInstance]
return
}
flash.message = "${message(code: 'default.created.message', args: [message(code: 'person.label', default: 'Person'), personInstance.id])}"
redirect action: 'show', id: personInstance.id
}
and add this just before your <g:form>
:
<g:hasErrors bean="${personInstance}">
<div class="errors">
<g:renderErrors bean="${personInstance}" as="list" />
</div>
</g:hasErrors>
Consider running grails generate-all packagename.Person
and editing the generated controller and GSPs. Of course move yours out of the way first.
You have to set either:
View:
render view:"/example"
Or:
render "${personInstance.name} successfully inserted"