Grails,我如何得到一个不保存的对象 [英] Grails , how do I get an object NOT to save

查看:104
本文介绍了Grails,我如何得到一个不保存的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是grails的新手,并试图创建一个表单,允许用户更改与我创建的网站相关的电子邮件地址。



它要求用户输入他们当前的密码以及他们想要使用的新电子邮件地址。
如果用户输入了错误的密码或无效的电子邮件地址,则应该用适当的错误信息拒绝它们。

现在,电子邮件验证可以通过grails中的约束来完成,但密码更改必须与当前密码匹配。

请参阅下面的代码:

 

def saveEmail =
{
def client = ClientUser.get(session.clientUserID)
client.email = params.email
if(clientUserService.checkPassword( session.clientUserID,params.password)== false)
{
flash.message =密码错误
client.discard()
重定向(action:'changeEmail')

else if(!client.validate())
{
flash.message =电子邮件地址无效
重定向(操作:'changeEmail')
}
else
{
client.save();
session.clientUserID = null;
flash.message =您的电子邮件地址已更改,请重新登录
重定向(controller:'clientLogin',action:'index')
}
}

现在我注意到很奇怪的是,如果我输入了无效的电子邮件,那么它不会保存更改如预期的那样),但如果我输入了错误的密码和有效的电子邮件,那么它会保存更改,甚至将它们写回数据库,即使它会给出正确的无效密码错误消息。



我很困惑,所以在所有if / else if / else块中设置了断点,并发现它按预期击中了第一个if语句,而没有击中其他语句,所以它永远不会碰到一个调用到save()方法,但它仍然被保存。

经过一些研究,我发现了一些关于discard()方法的文档,你可以在代码中看到它以上。所以我加了这个,但仍然无济于事。我甚至尝试使用丢弃,然后再次从数据库重新加载客户端对象,但仍然没有骰子。



这非常令人沮丧,我会感谢任何帮助,因为我认为这应该不是一个复杂的要求!

解决方案

Grails会在Web请求结束时关闭您的Hibernate会话,清除已更改的对象。该对象连接到Hibernate会话,因为您通过Hibernate获取了它( get())。如果您想避免更改刷新,您需要使用 discard()



这是自动完成的通过一个失败的验证器,这就是为什么你不必为验证失败做到这一点。



然而,通过移动这个逻辑来简化代码到您的 ClientUser 字段中的自定义验证器,这些字段会在失败时自动丢弃对象,或者使用Grails命令对象,也可以封装验证逻辑。然后,您只需检查命令对象上的错误。


I am new to grails and trying to create a form which allows a user to change the email address associated with his/her account for a site I am creating.

It asks for the user for their current password and also for the new email address they want to use. If the user enters the wrong password or an invalid email address then it should reject them with an appropriate error message.

Now the email validation can be done through constraints in grails, but the password change has to match their current password. I have implemented this check as a method on a service class.

See code below:

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 what I noticed that was odd was that if I entered an invalid email then it would not save the changes (as expected) BUT if I entered the wrong password and a valid email then it would save the changes and even write them back into the database even though it would give the correct "invalid password" error message.

I was puzzled so set break points in all the if/else if/else blocks and found that it was hitting the first if statement as expected and not hitting the others , so it would never come accross a call to the save() method, yet it was saved anyway.

After a little research I came accross documentation for the discard() method which you can see used in the code above. So I added this but still no avail. I even tried using discard then reloading the client object from the DB again but still no dice.

This is very frustrating and I would be grateful for any help, since I think that this should surely not be a complicated requirement!

解决方案

Grails closes your Hibernate session at the end of the web request, which will flush out the changed object. The object's connected to your Hibernate session because you got hold of it via Hibernate (get()). If you want to avoid having the change flushed, you need to use discard().

This is done automatically by a failing validator, which is why you're not having to do it for a validation fail.

You'd simplify the code, however, by either moving this logic to a custom validator on one of your ClientUser fields, which would automatically discard the object on failure, or by using a Grails command object, which could also encapsulate the verification logic. Then you'd just check for errors on the command object.

这篇关于Grails,我如何得到一个不保存的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆