Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6873899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:07:38+00:00 2026-05-27T04:07:38+00:00

I use Grails 2.0.0.RC2 and I have an User class like this: class User

  • 0

I use Grails 2.0.0.RC2 and I have an User class like this:

class User {
    String username
    String password

    // Idea from http://grailsrecipes.wordpress.com/2009/04/19/grails-user-registration-and-login/ .
    String formPassword
    String formPasswordConfirm

    // Constraints and validation ommited

    static transients = ['formPassword', 'formPasswordConfirm']

    def beforeUpdate() {
        println("Inside beforeUpdate")
        if (formPassword != null)
            encodePassword()
    }

    protected void encodePassword() {
        password = formPassword // Just for this case
    }
}

When user asks for his password reset, I send him email with a link to reset password page. Reset password form is simple – it contains only two fields: formPassword and formPasswordConfirm. I do a simple action in controller: user.formPassword = params["formPassword"] and user.formPasswordConfirm = params["formPasswordConfirm"]. Then I do (user.save()) – and problem begins.

My problem is that beforeUpdate() is not called. I thought it was a validation problem (it’s omiited here), but it’s not. As it turns out user.save() didn’t persist user to database! Why? I wanted it to be persisted, password should be changed. But user.isDirty() is false just before user.save(). That’s because none of persistent properties were changed. That’s true. Since user.save() wasn’t called – beforeUpdate() wasn’t called either.

Is this desired behaviour? Maybe it’s a Grails bug and beforeUpdate() should be always called before update and then isDirty() should be checked? What do you think?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:07:39+00:00Added an answer on May 27, 2026 at 4:07 am

    I don’t know if beforeUpdate should be called when transient fields are updated, but given the choice between

    1. it’s a Grails/Hibernate bug
    2. this is the expected behaviour because no DB updates would happen when transient fields are modified

    I would bet a lot of money on 2, and very little on 1.

    So leaving that aside, I think you’ve made your domain class a bit more complicated that it needs to be. Specifically, you should be able to achieve your goal with 2 password fields instead of 3.

    class User {
    
        static transients = ['passwordConfirm']
        def springSecurityService
    
        String password
        String passwordConfirm
    
        static constraints = {
            password blank: false, validator: {password, self ->
    
                // We only need to check the password confirmation when it is not empty, i.e.
                // when a user registers or resets their password
                if (self.passwordConfirm) {
                    password == self.passwordConfirm
                }
            }
        }
    
        def beforeInsert() {
            encodePassword()
        }
    
        def beforeUpdate() {
            if (isDirty('password')) {
                encodePassword()
            }
        }
    
        private void encodePassword() {
            password = springSecurityService.encodePassword(password)
            passwordConfirm = springSecurityService.encodePassword(passwordConfirm)
        }
    
    }
    

    Your reset password controller action should look like this:

    def resetPassword = {
    
        User user = User.findByUsername(params.username)
        user.password = params.formPassword
        user.passwordConfirm = params.formPasswordConfirm
    
        if (user.save()) {
            // It worked, send them to the login page or whatever...
        } else {
            // Validation failed, send them back to the reset password page    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several categories that I use in my Grails plugin. e.g., class Foo
For my Grails App i use Searchable Plugin to have an nice google-like Search.
I have a password protected internal maven repository I'd like to use to resolve
I need to use EJB remote calls from my grails application. For this in
My current requirement is: I have to package my grails app and use teamcity
Using Grails and CXF, I have published a small web service that looks like
Why is this happening when I use the grails help feature? nobody@nobody-desktop:~$ grails help
I'm learning to use Grails and have run into a situation I don't understand
when I use hsqldb with grails, it seems that each domain class gets its
In grails if you want to map to particular HTTP methods you normally use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.