Not sure what I am doing wrong here. I am hoping that it is something subtle. I can’t seem to pass errors to my gsp page. I have the following code in my controller:
def submit = {
if (params) { // if there are parameters
def sampleInstance = new Sample(params)// new sample
if (sample.validate()) { // try to validate
sample.save()
flash.message = "Successfully Entered Sample"
redirect ( action: 'sample' )
}else{
flash.message = "Error Entering Sample"
sampleInstance.errors.each {
println it
}
redirect ( action: 'sample', model:[sampleInstance:sampleInstance])
}
}
}
I have verified that params is not null. Failed validation creates hasErrors() which also has been verified and the code sample.errors.each {println it} notifies me of the correct Field errors as I would expect. But something is wrong with my redirect syntax maybe? because the flash.message will work, but I cannot access the model:[sampleInstance:sampleInstance] map and no errors are rendered.
Here is the code in my gsp:
<g:hasErrors>
<div class="errors">
<g:renderErrors bean="${sampleInstance}" as="list" />
</div>
</g:hasErrors>
My controller is called SubmitSampleController, the action is named submit, and the gsp page is called sample.gsp.
This may be an answer to my issue: I have another action called sample, maybe I need to perform all my logic inside the sample action, instead of inside the submit action? or is there a way to pass a model from one action to another action inside the same controller? I have a feeling my original model is getting lost.
def sample(){
def now = new Date()// today's date
def today = com.Sample.findAllBySampleReceivedDateGreaterThanEquals(now.clearTime())// finds all samples submitted today
[checkDate:today, date: now] // passes a map of checkDate and todays date to the sample.gsp page
}
In a redirect, the model is used as query parameters which is not that same as render, which does an actual forward and puts the objects in the request scope. What you need to do if there are errors is render a view and pass the model. Then you will get the desired output.