我是Grails的新手,我的情况是我要将智利元素限制为5。
所以使用大小约束。
static constraints = {
favoriteEplCircuitIds size 1..5
}
And static hasMany = [favoriteEplCircuitIds: String]
现在在我的测试用例中,当我使用父级时,我期待'size.toobig'。。错误['child']
但是我得到了一个太长的例外......如何压制它?
@Burt:根据您的建议,我使用了
assert 'size.toobig' == obj.errors.getFieldError('favoriteEplCircuitIds')
...但仍然收到以下长错误消息。
Error Message : Field error in object 'com.src.UserSecurity' on field 'favoriteEplCircuitIds': rejected value [[a, b, c, d, e, f]];
codes [com.src.UserSecurity.favoriteEplCircuitIds.size.error.com.src.UserSecurity.favoriteEplCircuitIds,
com.src.UserSecurity.favoriteEplCircuitIds.size.error.favoriteEplCircuitIds,com.src.UserSecurity.
favoriteEplCircuitIds.size.error.java.util.Set,com.src.UserSecurity.favoriteEplCircuitIds.size.error,userSecurity.
favoriteEplCircuitIds.size.error.com.src.UserSecurity.favoriteEplCircuitIds,userSecurity.favoriteEplCircuitIds.size.error.
favoriteEplCircuitIds,userSecurity.favoriteEplCircuitIds.size.error.java.util.Set,userSecurity.favoriteEplCircuitIds.size.error,
com.src.UserSecurity.favoriteEplCircuitIds.size.toobig.com.src.UserSecurity.favoriteEplCircuitIds,com.src.
UserSecurity.favoriteEplCircuitIds.size.toobig.favoriteEplCircuitIds,com.src.UserSecurity.favoriteEplCircuitIds.size.toobig.
java.util.Set,com.src.UserSecurity.favoriteEplCircuitIds.size.toobig,userSecurity.favoriteEplCircuitIds.size.toobig.com.twc.
eeportal.UserSecurity.favoriteEplCircuitIds,userSecurity.favoriteEplCircuitIds.size.toobig.favoriteEplCircuitIds,userSecurity.
favoriteEplCircuitIds.size.toobig.java.util.Set,userSecurity.favoriteEplCircuitIds.size.toobig,size.toobig.com.src.UserSecurity.
favoriteEplCircuitIds,size.toobig.favoriteEplCircuitIds,size.toobig.java.util.Set,size.toobig]; arguments **[favoriteEplCircuitIds,class
com.src.UserSecurity,[a, b, c, d, e, f],0,5]; default message [Property [{0}] of class [{1}] with value [{2}]
does not fall within the valid size range from [{3}] to [{4}]]'.**
答案 0 :(得分:1)
语法错误。它应该是
static constraints = {
child size: 1..5
}
假设你有
static hasMany = [child: SomeOtherDomainClass]
尽管多数化集合名称会更自然,例如“孩子”。但那是化妆品,与你所看到的问题毫无关系。
修改强>
您可以使用
访问错误def error = parent.errors.getFieldError('children')
但是toString()的可读性不高。要获得您在GSP中看到的错误,依赖注入“messageSource”bean,例如
def messageSource
然后您可以使用
获取错误消息String message = messageSource.getMessage(error, Locale.default)
它应该是这样的:
Property [children] of class [class Parent] with value [[c1, c2, c3, c4, c5, c6]] does not fall within the valid size range from [1] to [5]