Grails databinding problem
What is missing here because when a validation fail the rendered page doesnt show the previous introduced values so the user cant correct it. Please update my code with the missing code! Everything is working properly, except when returning after a validation fail the previous introduced values are gone.
add.gsp
<div id="stylized" class="myform">
<g:form controller="conference" action="save">
<h1>Add New Conference Record</h1>
<p>Basic Information</p>
<label>Name
<span class="small">Add your name</span>
</label>
<input type="text" name="name" /><br>
...
...
<p>Dates</p>
<label>Start Date
<span class="small">First Day</span>
</label>
<g:datePicker name="startDate" precision="day" value="${new Date()}"/><br><br>
<label>End Date
<span class="small">First Day</span>
</label>
<g:datePicker name="endDate" precision="day" value="${new Date()}"/><br><br>
...
...
<g:submitButton name="save" value="Save" id="addConference"/>
<div class="spacer"></div>
</g:form>
<g:hasErrors bean="${conferenceInstance}">
<div class="errors">
<g:renderErrors bean="${conferenceInstance}" as="list" />
</div>
</g:hasErrors>
</div>
ConferenceController
def save = {
def conferenceInstance = new Conference(params)
if (conferenceInstance.save(flush: true)) {
flash.message = "${message(code: 'default.created.message', args: [message(code: 'conference.label', default: 'Cferenceon'), conferenceInstance.id])}"
redirect(action: "/index")
}
开发者_运维知识库 else {
render(view: "add", model: [conferenceInstance: conferenceInstance])
}
}
"Teach a Man to Fish" Answer
Grails scaffolding shows the previous values when validation fails, so the easiest way to see how this should work would be to run:
grails generate-all Conference
Then study the code of the generated controller and GSPs.
Spoon-Feeding Answer
The problem is in your GSP code, if you specify
<g:datePicker name="startDate" precision="day" value="${new Date()}"/>
the date-picker will always shows today's date. Similarly, this text field will always be blank:
<input type="text" name="name" />
because you have not specified a value
attribute.
When validation fails, you need to get the values from the bean that failed validation, so replace the above with:
<g:datePicker name="startDate" precision="day" value="${conferenceInstance?.date}"/>
and
<input type="text" name="name" value="${conferenceInstance?.name}"/>
Let me know if that works..
<div id="stylized" class="myform">
<g:form controller="conference" action="save">
<h1>Add New Conference Record</h1>
<p>Basic Information</p>
<label>Name
<span class="small">Add your name</span>
</label>
<input type="text" name="name" value="${conferenceInstance.name}/><br>
...
...
<p>Dates</p>
<label>Start Date
<span class="small">First Day</span>
</label>
<g:datePicker name="startDate" precision="day" value="${conferenceInstance.startDate}"/><br><br>
<label>End Date
<span class="small">First Day</span>
</label>
<g:datePicker name="endDate" precision="day" value="${conferenceInstance.endDate}"/><br><br>
...
...
<g:submitButton name="save" value="Save" id="addConference"/>
<div class="spacer"></div>
</g:form>
<g:hasErrors bean="${conferenceInstance}">
<div class="errors">
<g:renderErrors bean="${conferenceInstance}" as="list" />
</div>
</g:hasErrors>
</div>
精彩评论