Grails transient properties not picked up on object creation
Asked Answered
U

2

12

After migrating from Grails 1.3.7 to 2.0.4 I have noticed a problem with one of my domain classes where I use transient properties in order to handle passwords.

My domain class looks like this (simplified):

   package test

   class User {
String email 
String password1
String password2
//ShiroUser shiroUser

static constraints = {
    email(email:true, nullable:false, unique:true)
    password1(nullable:true,size:5..30, blank: false, validator: {password, obj ->

        if(password==null && !obj.properties['id']){
          return ['no.password']
        }
        else return true
      })
    password2(nullable:true, blank: false, validator: {password, obj ->
         def password1 = obj.properties['password1']

         if(password == null && !obj.properties['id']){
          return ['no.password']
        }
        else{
          password == password1 ? true : ['invalid.matching.passwords']
        }
      })

}
static transients = ['password1','password2']
   }

In 1.3.7 this used to work in my Bootstrap:

    def user1= new User (email: "[email protected]", password1: "123456", password2: "123456")
    user1.save()

However, in Grails 2.0.x this will result in error stating that password1 and password2 are both null. The same thing happens in my controllers if I try to do:

    def user2= new User (params)// params include email,password1 and password2 

In order to make it work I have to do the following workaround:

    def user2= new User (params)// params include email,password1 and password2 
    user2.password1=params.password1
    user2.password2=params.password2
    user2.save()

This is quite ugly - and annoying.

Can anyone say if my usage of transients has become invalid in grails 2.x, or if this could be a framework bug of some kind?

Unwonted answered 2/7, 2012 at 16:14 Comment(0)
M
15

For security reasons transients are no longer auto-bound. But you can easily make it work by adding in a 'bindable' constraint (see http://grails.org/doc/latest/ref/Constraints/bindable.html). Change

password2(nullable:true, blank: false, validator: {password, obj ->

to

password2(bindable: true, nullable:true, blank: false, validator: {password, obj ->
Minetta answered 2/7, 2012 at 17:9 Comment(4)
Thanks Burt - Makes good sense with this security-related change. Was just not aware of the (new?) 'bindable' constraint.Unwonted
Hey Burt, is this changed in 2.1.0? I have an exact same scenario and for us "cnfPassword" still comes as null! Don't want to open a new thread for the same question :)Megalith
Hi Burt, it is not working properly in grails 2.4.3. Is there any alternative solution that you can advice?Copy
This does not seem to work in Grails 2.3.9. If the property is transient, bindable: true will not make it bound.Katar
V
3

I think as the part of data binding improvement in grails 2.x - it won't bind transient properties.

Vitavitaceous answered 2/7, 2012 at 16:20 Comment(1)
is there an alternate way to do this then?Wearisome

© 2022 - 2024 — McMap. All rights reserved.