简体   繁体   中英

Grails custom validation message, error

I was trying to add a custom message to my validator like this:

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

Of course I adjusted the messages.properties file. I'm getting the following exception:

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

I dont really understand why there is a connection to usermanagement.UserRole.exists()?

EDIT: UserRole is generated by the SpringSecurity core Plugin EDIT: 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

           }
}

I just saw that the statement in the validator is wrong:

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

should be

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

Because of that the joining I set in the Bootstrap.groovy was not valid which should be the root of the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM