validate()和hasErrors()之间的连接是什么? [英] What is the connection between validate() and hasErrors()

查看:203
本文介绍了validate()和hasErrors()之间的连接是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题来自另一个问题

我的。在那个问题中,我遇到了 hasErrors()函数不适用于非持久化域类的情况,即使在所有事情我确实遵循了教学法第7.5部分。 p>

遵循Victor的方法,我通过调用validate()来解决问题,但我不明白它为什么起作用。 Grails文档似乎没有提到你应该在hasErrors()函数之前调用validate()。这怎么会发生呢?

解决方案

对我来说, validate 在询问一个对象是否为 hasErrors (或 save >)来调用适当的域对象之前需要调用它, code> validate )。在这种情况下验证的意思是检查这个对象是否有效,如果不是,则指示任何错误。

另外,GORM实现必须调用 validate每次对对象进行任何更改时,对我而言这都是不太理想的行为,因为它可能涉及许多工作经常和不必要地完成(其中一些限制可能涉及大量工作) 。

7.2节的开头非常清楚:要验证域类,您可以在任何实例上调用验证方法。它还指出在Grails中基本上有两个验证阶段,第一个阶段是数据绑定,当您将请求参数绑定到实例上时发生数据绑定......在这一点上,您可能已经在错误属性中存在错误,这是由于类型转换(例如将字符串转换为日期)您可以使用错误API检查这些并获取原始输入值......验证的第二阶段发生在您调用验证或保存时,这是在Grails验证绑定



hasErrors 的文档也提到了这一点。您可以在文档站点。我总是会建议您查看这些以及更具描述性的用户指南页面,因为它们通常会提供更多的细节。
以下是 validate method

我从来没有遇到过调用 validate 直接 - 对我来说非常清楚,我可以选择完成所有工作的位置,并且我已准备好进行验证。我看不到在任何地方改变这种行为的选项,但是如果你想自动或在特定条件下调用 validate ,你可以使用一些Groovy元编程魔术可能会向类中添加 invokeMethod ,并在传递某些调用之前调用 validate 。看看此处这里
(不知道我会建议,但请记住,您的类现在将依赖于在GORM验证框架中使用,因为 validate 方法可能会不存在)。


This question comes from the problem of another question of mine. In that question, I come across a situation that hasErrors() function doesn't work for non-persistent domain class, even after all the things I did following the instruction, part 7.5 .

Following Victor's way, I fixed the problem by calling validate(), but I don't understand why it works. The Grails documents seem to say nothing about you should call a validate() before hasErrors() function. How could this happen?

解决方案

It does make sense to me that validate would need to be called before asking an object whether it hasErrors (or save for proper domain objects, which calls validate under the covers). Validate in this context means "check whether this object is valid and indicate any errors if not".

Alternatively the GORM implementation would have to call validate every time any change is made to an object, which to me would be less desirable behaviour, as it might involve lots of work being done often and unnecessarily (some of those constraints could involve a lot of work).

The beginning of section 7.2 states pretty clearly "To validate a domain class you can call the validate method on any instance". It also states that "within Grails there are essentially 2 phases of validation, the first phase is data binding which occurs when you bind request parameters onto an instance such as... At this point you may already have errors in the errors property due to type conversion (such as converting Strings to Dates). You can check these and obtain the original input value using the Errors API. ... The second phase of validation happens when you call validate or save. This is when Grails will validate the bound values againts the constraints you defined."

The documentation for hasErrors also mentions this. You can access this by finding the method call in the navigation frame on the left, when you are on the documentation site. I would always recommend looking on these as well as the more descriptive user guide pages, as they often give a little more detail. Here's the page for the validate method too.

I've never had a problem calling validate directly - it's very clear to me and I can choose the point where all the work is done and I'm ready for the validation to take place. I can't see an option to change this behaviour anywhere, but if you wanted validate to be called automatically or under certain conditions, you could perhaps use some Groovy meta programming magic by maybe adding invokeMethod to the class and have it call validate before passing certain calls on. Have a look here and here. (Not sure I would recommend that though! And bear in mind your class would now be dependent on being used within the GORM validation framework, as that validate method might not otherwise exist).

这篇关于validate()和hasErrors()之间的连接是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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