首页 > 解决方案 > 使用 grails 3.3 在命令对象中使用共享约束

问题描述

我在将旧单元测试从 grails 2.5.4 迁移到 grails 3.3 时遇到问题

我有很多使用共享首选项的命令。一个例子,de UserController里面的UserCommand:

class UserCommand implements Validateable {
    String firstName

    static constraints = {
        firstName shared: 'nonNullableString'
    }
}

在 application.groovy 我以这种方式定义了约束:

grails.gorm.default.constraints = {
    nonNullableString(blank: false, maxSize: 255)
}

我也将User域定义为:

class User {
    String firstName

    static constraints = {
        firstName shared: 'nonNullableString'
    }
}

写一个我做的测试:

User user = new User()
user.validate() 

它按预期工作。但是当我这样做时:

UserCommand command = new UserCommand()
command.validate()

它抛出一个异常。

`grails.gorm.validation.exceptions.ValidationConfigurationException: Property [User.firstName] references shared constraint [nonNullableString:null], which doesn't exist!`

我以为我在 application.groovy 文件中犯了一个错误,但共享约束适用于域..

这是一个具有相同问题的示例项目:https ://github.com/almejo/commandtest

有什么想法吗?

标签: grailsgrails-ormgrails-2.0grails-3.3.x

解决方案


推荐阅读