gpt4 book ai didi

grails - 自定义错误消息无法在带有grails的gorm中使用

转载 作者:行者123 更新时间:2023-12-02 14:23:47 25 4
gpt4 key购买 nike

似乎无法在Grails 2.3.7上获得自定义验证消息。

class Team {
int id
String email

static mapping = {
id column:'TEAM_ID', generator:'native'
email column:'TEAM_EMAIL'
}
static constraints = {
email unique:true
}

messages.properties (尝试了所有这些...)
team.email.unique=Email already used!
Team.email.unique=Email already used!
my.package.Team.email.unique=Email already used!

验证问题由我的服务处理,但是自定义错误消息从未使用:
Team t = new Team(...)
if(!t.validate())
println t.errors

输出...
Field error in object 'Team' on field 'email': rejected value.... ...
default message [Property [{0}] of class [{1}] with value [{2}] must be unique]

不知道是什么问题,根据 docs,约定是:
[Class Name].[Property Name].[Constraint Code]
额外配置

BuildConfig
dependencies {
compile('com.oracle:ojdbc6:11.2.0')
}
plugins {
build ":tomcat:7.0.52.1"
runtime ":hibernate:3.6.10.9"
}

数据源
 dialect  =  "org.hibernate.dialect.Oracle10gDialect"
hibernate {
cache.use_second_level_cache = false
cache.use_query_cache = false
cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory'
singleSession = false
}

最佳答案

根据http://grails.org/doc/2.3.7/ref/Constraints/unique.html上的唯一约束文档页面,它是className.propertyName.unique,但是println t.errors始终看起来像您所看到的-toString()结果不是很有用。要查看已解决/已翻译的消息,您可以测试您在测试或测试应用中的工作,并可以使用它来显示已解决的消息:

// dependency injection in controller/service/etc.
def messageSource

...

Locale locale = Locale.getDefault()
def stringsByField = [:].withDefault { [] }

for (fieldErrors in t.errors) {
for (error in fieldErrors.allErrors) {
String message = messageSource.getMessage(error, locale)
stringsByField[error.field] << message
}
}

println stringsByField

关于grails - 自定义错误消息无法在带有grails的gorm中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25595005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com