0

我不想保存已编辑的域类,而是想使用我已制定的更改创建它的新实例。

def update =
{
        def VariantInstance = Variant.get(params.id)
        VariantInstance.properties = params
        def NewVariantInstance = new Variant()
        //Now Assign Variant Instance new Values
        NewVariantInstance.Name = VariantInstance.Name
        NewVariantInstance.LocationID = VariantInstance.LocationID
        NewVariantInstance.aliases = VariantInstance.aliases
        NewVariantInstance.closeToBoundary = VariantInstance.closeToBoundary


        if (VariantInstance) {
        if (!VariantInstance.hasErrors() && !NewVariantInstance.hasErrors()) {
            println("no errors")
            try {
            NewVariantInstance.save()
            }
            catch (Exception ex)
            {
                println(ex.toString())
            }


            flash.message = "${message(code: 'default.updated.message', args: [message(code: 'Variant.uniqueIdentifyingName', default: 'Variant'), VariantInstance.id])}"
            redirect(action: "list")
        }

}

但是,当我运行此 A) 未保存新版本和 B) NewVariantInstance 没有 id。想法?

4

1 回答 1

2

你的VariantInstanceandNewVariantInstance永远不会被验证,所以hasErrors()总是会返回 false。你需要从

!VariantInstance.hasErrors() && !NewVariantInstance.hasErrors()

VariantInstance.validate() && NewVariantInstance.validate()

可能NewVariantInstance存在验证错误,因此save()不成功。

于 2013-02-28T21:11:12.663 回答