Grails自定义验证消息,错误

时间:2015-04-30 14:58:54

标签: validation grails message

我正在尝试向我的验证器添加自定义消息,如下所示:

static constraints = {
        joining validator: { val, obj ->
            if(val?.after(obj.birthday)) return 'joining.error'
        }
    }

当然我调整了messages.properties文件。我得到以下例外:

2015-04-30 16:52:27,253 [localhost-startStop-1] ERROR context.GrailsContextLoaderListener  - Error initializing the application: No signature of method: usermanagement.UserRole.exists() is applicable for argument types: (null, java.lang.Long) values: [null, 1]
Possible solutions: exists(long, long), exists(java.io.Serializable), list(), first(), wait(), last()
Message: No signature of method: usermanagement.UserRole.exists() is applicable for argument types: (null, java.lang.Long) values: [null, 1]
Possible solutions: exists(long, long), exists(java.io.Serializable), list(), first(), wait(), last()
    Line | Method
->>   92 | methodMissing                    in org.grails.datastore.gorm.GormStaticApi
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|     86 | doCall                           in usermanagement.UserRole$__clinit__closure9$_closure14$_closure15
|     85 | doCall . . . . . . . . . . . . . in usermanagement.UserRole$__clinit__closure9$_closure14
|     44 | create                           in usermanagement.UserRole
|     19 | doCall . . . . . . . . . . . . . in BootStrap$_closure1
|    327 | evaluateEnvironmentSpecificBlock in grails.util.Environment
|    320 | executeForEnvironment . . . . .  in     ''
|    296 | executeForCurrentEnvironment     in     ''
|    266 | run . . . . . . . . . . . . . .  in java.util.concurrent.FutureTask
|   1142 | runWorker                        in java.util.concurrent.ThreadPoolExecutor
|    617 | run . . . . . . . . . . . . . .  in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run                              in java.lang.Thread

我真的不明白为什么有与usermanagement.UserRole.exists()的连接?

编辑:UserRole由SpringSecurity核心插件生成 编辑:Bootstrap.groovy:

import java.text.SimpleDateFormat
import usermanagement.*
class BootStrap {

    def init = { servletContext ->

              def adminRole = new Role(authority: 'ROLE_ADMIN').save(flush: true)
              def userRole = new Role(authority: 'ROLE_USER').save(flush: true)

              def birthday_sdf = new SimpleDateFormat("dd-M-yyyy hh:mm:ss")
              def birthday_str = "31-08-1982 10:20:56"

              def testUser = new User(username: 'me', password: 'password',
                                      firstName: 'Adam', lastName: 'Administrator',
                                      role: adminRole, email: 'test@gmail.com',
                                      birthday: birthday_sdf.parse(birthday_str), joining: new Date())
              testUser.save(flush: true)

              UserRole.create testUser, adminRole, true

           }
}

1 个答案:

答案 0 :(得分:0)

我刚看到验证器中的声明是错误的:

if(val?.after(obj.birthday)) return 'joining.error'

应该是

if(!(val?.after(obj.birthday))) return 'joining.error'

因为我在Bootstrap.groovy中设置的加入无效,这应该是问题的根源。