Grails: How to combine domain objects' errors with command objects' errors?

前端 未结 3 728
日久生厌
日久生厌 2021-02-05 17:06

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
相关标签:
3条回答
  • 2021-02-05 17:41

    I did the following for my project and found it to be more cleaner!

    domain.errors.each {
      cmdObject.errors.reject(it.code, g.message(error: it))
    } 
    
    0 讨论(0)
  • 2021-02-05 17:43

    I think the full answer is:

    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
    }
    
    0 讨论(0)
  • 2021-02-05 17:51

    You could probably use the reject mechanism, i.e.

    domainObjects.errors.each{
         commandObject.errors.reject( ... )
    }
    

    http://grails.org/doc/1.3.7/ref/Domain%20Classes/errors.html

    0 讨论(0)
提交回复
热议问题