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

时间:2015-01-18 15:04:44

标签: validation grails transient

我找不到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()就可以了。

1 个答案:

答案 0 :(得分:0)

出于安全考虑,瞬态不再是自动绑​​定的。但是您可以通过添加“可绑定”约束来轻松地使其工作(请参阅http://grails.org/doc/latest/ref/Constraints/bindable.html)。您可以像这样使用confirmPassword。

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