Grails:如何将域对象的错误与命令对象的错误结合起来? [英] Grails: How to combine domain objects' errors with command objects' errors?

查看:28
本文介绍了Grails:如何将域对象的错误与命令对象的错误结合起来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有 User 域类和 RegistrationCommand 类.因此,当用户在网站上注册时,数据验证有两个步骤:

Suppose I have User domain class and RegistrationCommand class. So when user is registering on website there are two steps for data validation:

  1. RegistrationCommand 约束验证.
  2. 用户域对象限制验证.

控制器动作接收命令对象并在计算后将其传递给模型中的视图.所以,我想将域对象的验证错误与命令对象的错误结合起来,并将它们作为命令对象的一部分传递.最好的方法是什么?

Controller action receive command object and pass it to view in a model after computing. So, I want to join domain objects' validation errors with command objects' errors and pass them as a part of command object. What is the best way to do that?

推荐答案

我认为完整的答案是:

if (!user.validate() || !user.save(true))
{
    if (user.errors.hasErrors())
    {
        user.errors.allErrors.each {FieldError error ->
            final String field = error.field?.replace('profile.', '')
            final String code = "registrationCommand.$field.$error.code"
            command.errors.rejectValue(field, code)
        }
    }
    chain(action: 'registration', model: [command: command])
    return
}

这篇关于Grails:如何将域对象的错误与命令对象的错误结合起来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆