grails枚举类型验证无法正常工作 [英] grails enum type validation not working as expected
本文介绍了grails枚举类型验证无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个单元测试是违规违规,不应该在哪里。我正在测试如果有效的枚举设置为域类的变量。
I have a unit test that is failing on a constraint violation, where it should not be. I'm testing if valid enums are set as variables of a domain class.
我的枚举:
public enum GenderPreference {
M('Male'),
F('Female'),
A('Any')
final String value
GenderPreference(String value) {
this.value = value
}
public String toString() {
value
}
public String getKey() {
name()
}
public String getValue() {
value
}
}
我的域名:
class Profile {
GenderPreference genderPreference
static constraints = {
genderPreference (blank:true, nullable:true)
}
}
我的单元测试:
罚款:
def instance = new Profile(genderPreference: GenderPreference.M)
assertTrue instance.validate(['genderPreference'])
这应该会失败但不会。 QQQ不是有效的枚举
this should fail but does not. QQQ is not a valid enum
instance = new Profile(genderPreference: 'QQQ')
assertFalse instance.validate(['genderPreference'])
我正在使用grails 2.2.4。我认为在旧版本中,还有其他一些东西必须添加到枚举的域约束,但我认为这应该工作。缺少什么?
I'm using grails 2.2.4. I think in older versions there was something else that had to be added to the domain constraints for enums, but I thought this should work. What's missing?
推荐答案
您可以使用inList向您的枚举添加约束
You can use inList to add constraints to your enum
static constraints = {
genderPreference blank:true, nullable:true,
inList: GenderPreference.values() as List
}
这篇关于grails枚举类型验证无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文