Grails:自定义验证器中无法访问瞬变变量?

Grails: transients variable not accessible in custom validator?

我找不到 obj.confirmPassword 总是空的原因。 如果 confirmPassword 不是瞬态的一部分,一切正常。

String confirmPassword
boolean enabled = true
boolean accountExpired
boolean accountLocked
boolean passwordExpired

static transients = ['springSecurityService', 'confirmPassword']

static constraints = {
   username blank: false, unique: true, minSize: 4, maxSize: 64
   password blank: false, nullable: false, minSize: 4, maxSize: 64, validator: {password, obj ->
      def password2 = obj.confirmPassword
      password2 == password ? true : ['invalid.matchingpasswords']
   }
   userFullname blank: false, minSize: 4, maxSize: 64
}

这个在调用 save() 时抛出 NullPointer:

String confirmPasswordField
    static transients = ['springSecurityService', 'confirmPasswordField']

    static constraints = {
        confirmPasswordField bindable: true, nullable:true, blank: false
        username blank: false, unique: true, minSize: 4, maxSize: 64
        password blank: false, nullable: false, minSize: 4, maxSize: 64, validator: {password, obj ->
            def confirmPassword = obj.confirmPasswordField
            confirmPassword == password ? true : ['invalid.matchingpasswords']
        }
        userFullname blank: false, minSize: 4, maxSize: 64
    }

如果我评论关于 confirmPasswordField 的所有内容 - save() 没问题。

出于安全原因,瞬态不再自动绑定。但是您可以通过添加 'bindable' 约束(参见 http://grails.org/doc/latest/ref/Constraints/bindable.html)轻松使其工作。您可以像这样使用 confirmPassword。

confirmPassword(bindable: true, nullable:true, blank: false)