Search code examples
grailscontrollers

grails controllers errors


I have this DomainClass:

package cm

class XXX{


    String city
    String website

    static constraints = {
     city(nullable: true)
     website(nullable: true)

    }

    static mapping = {
        id column:'xxx_id'
        city column: 'xxx_city'
        website column: 'xxx_website'
        table "xxx_XXX"
        version false

}
}

The Controller:

class ConferenceController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [XXXInstanceList: XXX.list(params), XXXInstanceTotal: XXX.count()]
    }

    def save = {
        def XXXInstance= new XXX(params)
        if (!XXXInstance.save(flush: true)) {
            render view: 'add', model: [XXXInstance: XXXInstance]
            return
        }

        //flash.message = "${message(code: 'default.created.message', args: [message(code: 'person.label', default: 'Person'), personInstance.id])}"
        redirect(uri:"/index.gsp")
    }

}

and my add.gsp page:

<head>

  <title> xXXx</title>
  <meta name="layout" content="main2" />


</head>

...


<g:form controller="XXX" action="save">

  <g:hasErrors bean="${XXXInstance}">
   <div class="errors">
      <g:renderErrors bean="${XXXInstance}" as="list" />
   </div>
</g:hasErrors>


    year
    <g:textField name="year" /><br>
    website
    <g:textField name="website" /><br>

    <g:submitButton name="save2" value="Save" />

     </g:form></div>
    </div>


              ...
</body>

With this current code, everything works fine, but when a contraint is failed, it is showns the corresponding error but the written values are gone.. I cant figure this out - i'v tried a lot of things, but im sure the solution is as easy as my question -.- Please help.


Solution

  • I think you should check the *.properties file in YOUR_PROJECT\grails-app\i18n folder. They have the definition for translation. Investigate it for a time and refer to the document if need, you will understand how Grails perform translation.

    And uncomment the following line in your controller:

    //flash.message = "${message(code: 'default.created.message', args: [message(code: 'person.label', default: 'Person'), personInstance.id])}"