Eddard Stark
Eddard Stark

Reputation: 3595

Change domain's property during save in grails 2.3

I am trying to modify or add a domain property during save action of a controller. I am using grails 2.3.2 and my code is as follows :

@Transactional
def save(Stock stockInstance) {
    if (stockInstance == null) {
        notFound()
        return
    }
    stockInstance.stockBy = User.findById(springSecurityService.getPrincipal().id)

    if (stockInstance.hasErrors()) {
        respond stockInstance.errors, view: 'create'
        return
    }

    stockInstance.save flush: true

    request.withFormat {
        form {
            flash.message = message(code: 'default.created.message', args: [message(code: 'stockInstance.label', default: 'Stock'), stockInstance.id])
            redirect stockInstance
        }
        '*' { respond stockInstance, [status: CREATED] }
    }
}

The problem is, 'stockBy' property is comming up null. The springsecurityservice is returning a value but it is not being set in the property stockBy. This code worked fine in older versions of grails. Why is this not working for grails 2.3.2 ?

Upvotes: 1

Views: 432

Answers (1)

mlist
mlist

Reputation: 285

I have struggled with the same issue. The problem is that the domain object remembers the previous errors and needs to be re-validated after adding the User to stockInstance.stockBy:

   stockInstance.stockBy = springSecurityService.currentUser
   stockInstance.validate()

Upvotes: 1

Related Questions