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

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

问题描述

假设我有User domain类和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天全站免登陆