从2.3.4移动到2.3.5后,Grails Constraints不会创建视图验证错误

时间:2014-01-31 23:07:57

标签: grails gorm

我是GRAILS的新手,使用2.3.5。

我有一个相当简单的域对象:

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
   }
}

然后我有以下控制器保存操作:

 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]}
        }
    }
}

无论我使用什么约束,我都会得到预期的结果,即数据库插入不会成功,但我不会将错误捕获并发送回“创建”视图,就像我期望的那样,我得到了带有SERVER 500的error.gsp页面错误说明异常和失败的查询。我缺少一个设置吗?我只是使用H2 DB。

1 个答案:

答案 0 :(得分:0)

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'
}