Java – Grails, how do I get an object without saving it

I'm new Grails and trying to create a form that allows the user to change the email address associated with his / her account. I'm creating a website

It requires users to their current password and the new email address they want to use If the user enters an incorrect password or invalid email address, it should reject them with the appropriate error message

Email authentication can now be done through restrictions in Grails, but the password change must match its current password I have implemented this check as a method on the service class

See the following code:

def saveEmail =
{
    def client = ClientUser.get(session.clientUserID)
    client.email = params.email
    if(clientUserService.checkPassword(session.clientUserID,params.password) ==false)
    {
        flash.message = "Incorrect Password"
        client.discard()
        redirect(action:'changeEmail')
    }    
    else if(!client.validate())
    {
         flash.message = "Invalid Email Address"
         redirect(action:'changeEmail')
    }
    else
    {
        client.save();
        session.clientUserID = null;
        flash.message = "Your email address has been changed,please login again"
        redirect(controller: 'clientLogin',action:'index')
    }
}

Now I notice that it is strange that if I enter an invalid email, it will not save the changes (as expected), but if I enter the wrong password and a valid email, it will save the changes and even write them back to the database, even if it will give the correct "invalid password" error message

I set a breakpoint in all if / else if / else blocks and found that it hit the first if statement in the expected way, not others, so it will never call the save () method, but it was saved

After some research, I got the document that can see the discard () method used in the above code So I added, but it's still useless I even tried to use discard and then reload the client object from the database, but there were still no dice

This is very frustrating and I would appreciate any help because I think it is definitely not a complex request!

Solution

Grails closes your hibernate session at the end of the web request, which will refresh the changed objects This object connects to your hibernate session because you keep it through hibernate (get()) If you want to avoid change refresh, you need to use discard()

This is done automatically by the failed verifier, which is why you don't have to do it for validation failure

However, you can simplify your code by moving this logic to a custom validator on one of your clientuser fields, which will automatically discard the object in case of failure, or use Grails command objects that can also encapsulate the validation logic Then you only need to check the error of the command object

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>