Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails transient properties not picked up on object creation

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?

like image 559
John Doppelmann Avatar asked Jul 02 '12 16:07

John Doppelmann


2 Answers

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 ->
like image 109
Burt Beckwith Avatar answered Oct 26 '22 05:10

Burt Beckwith


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

like image 41
Sudhir N Avatar answered Oct 26 '22 04:10

Sudhir N