validate() 和 hasErrors() 之间有什么联系
这个问题来自另一个问题的问题矿。在这个问题中,我遇到了一种情况,即 hasErrors()
函数不适用于非持久域类,即使在我按照 说明,第 7.5 部分。
按照 Victor 的方式,我通过调用 validate() 解决了问题,但我不明白它为什么有效。 Grails 文档似乎没有提到应该在 hasErrors() 函数之前调用 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对我来说,在询问对象是否有错误(或正确的域对象的
save
)之前需要调用validate
确实有意义在幕后调用validate
)。在此上下文中,验证的意思是“检查此对象是否有效,如果无效则指示任何错误”。或者,每次对对象进行任何更改时,GORM 实现都必须调用 validate ,这对我来说是不太理想的行为,因为它可能涉及经常且不必要地完成大量工作(某些这些限制可能涉及大量工作)。
第 7.2 节的开头非常清楚地指出“要验证域类,您可以在任何实例上调用 validate 方法”。它还指出“在 Grails 中,验证基本上有 2 个阶段,第一个阶段是数据绑定,当您将请求参数绑定到实例上时发生,例如......此时,您可能已经在错误属性中出现了错误,因为类型转换(例如将字符串转换为日期)。您可以使用 Errors API 检查这些并获取原始输入值...当您调用 validate 或 save 时,验证的第二阶段就会发生。值反对您定义的约束。”
hasErrors
的文档也提到了这。当您位于文档站点。我总是建议查看这些以及更具描述性的用户指南页面,因为它们通常会提供更多细节。这是
validate
方法的页面 也是。我在直接调用
validate
时从未遇到过问题 - 这对我来说非常清楚,我可以选择完成所有工作并准备好进行验证的点。我看不到在任何地方更改此行为的选项,但如果您希望自动或在某些条件下调用validate
,您可以通过添加invokeMethod 来使用一些 Groovy 元编程魔法
到类并让它在传递某些调用之前调用validate
。看看这里和此处。(但不确定我是否会推荐这样做!请记住,您的类现在将依赖于在 GORM 验证框架中使用,因为
validate
方法可能不存在)。It does make sense to me that
validate
would need to be called before asking an object whether ithasErrors
(orsave
for proper domain objects, which callsvalidate
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 wantedvalidate
to be called automatically or under certain conditions, you could perhaps use some Groovy meta programming magic by maybe addinginvokeMethod
to the class and have it callvalidate
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).