Search code examples
grailsgrails-orm

Grails Constraints are not creating view validation errors after move from 2.3.4 to 2.3.5


I am fairly new to GRAILS, using 2.3.5.

I have a fairly simple domain object:

class Product {
   String name
   ProductType type
   ProductType subtype
   String desc

   static belongsTo = [organization: Organization]
   static constraints = {
     name blank: false
     type blank: false
     subtype blank: false
     desc nullable: true, maxSize:1000
   }
}

I then have the following for the controller save action:

 def save(Product productInstance) {

    if(!productInstance.save()) {
        respond productInstance.errors, view: 'create'
    } else {
        request.withFormat {
            form {
                flash.message = "Product '${productInstance.name}' was created successfully!"
            redirect controller: 'product', action: 'index'
            }
            '*' {respond productInstance, [status: CREATED]}
        }
    }
}

No matter what constraint I use, I get the expected result, which is the DB insert will not succeed, but I don't get the errors trapped and sent back to the "create" view like I expect, I get the error.gsp page with a SERVER 500 Error stating the exception and the failed query. Is there a setting I am missing? I am just using the H2 DB.


Solution

  • def save(Product product) {
        product.validate()    
        if (product.hasErrors()){
           log.error product.errors
           render view:'/product/create', model:[product:product]
           return
        }
    
        product.save(true)
        flash.message = "Product '${product.name}' was created successfully!"
        redirect controller: 'product', action: 'index'
    }