缺少命令类中的validate()方法

时间:2012-02-17 21:03:21

标签: validation grails

我很难搞清楚这个简单代码中的错误:

这是我更改密码的命令:

package myapp.commands

import org.springframework.web.context.request.RequestContextHolder as RCH
import myapp.User

class PasswordCommand {
String currentPassword
String password
String passwordConfirm
private u

User getUser() {
    def id = RCH.requestAttributes.session?.user?.id
    if (!u && id) {
        u = User.get(id)
    }
    return u
}

static constraints = {
    currentPassword blank: false, validator: { val, cmd ->
        if (cmd.user && cmd.user.password != val)
            return "user.password.invalid"
    }
...
}

在相应的控制器中,我使用此操作:

 def doPassword = { PasswordCommand cmd ->
    if (!cmd.hasErrors()) {
        User user = cmd.getUser()
           ...

不幸的是,当我尝试更改密码时收到错误500:

URI: /Myapp/user/doPassword
Class: java.lang.NoSuchMethodError
Message: PasswordCommand.validate()Z

发生什么事了?!

1 个答案:

答案 0 :(得分:1)

添加@Validateable注释:

import grails.validation.Validateable
...

@Validateable
class PasswordCommand {
...
}