使用内联验证方法和验证器类验证空字段
我有以下输入字段:
<h:inputText value=".." validator="#{labController.validateValue}"/>
如果该字段为空,则不会对其进行验证(不会调用 labController
中的 validateValue
)。
但是使用单独的验证器类:
<h:inputText value=".." >
<f:validator validatorId="labDateValidator"/>
</h:inputText>
那么即使输入字段为空,也可以调用它的 validate 方法吗?
这是我观察到的。此行为是否取决于实现或版本(我使用的是 Mojarra 2.1)?
背景是我想使用自己的方法/类来完成所有验证任务,包括所需的验证。它仅适用于验证器类吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在 JSF 中遇到了疏忽。
validator
封装在MethodExpressionValidator< /code>
根据其
validate()
方法确实在值为null
时跳过验证。这没有考虑新的 JSF 2.0
javax.faces.VALIDATE_EMPTY_FIELDS
上下文参数,该参数默认为true
。它实际上应该与UIInput#validate()
。这已被报告为 Mojarra 问题 1508。我已经投票并添加了新评论来唤醒他们。
You've hit an oversight in JSF. The
validator
is wrapped in aMethodExpressionValidator
which according to the source of itsvalidate()
method indeed skips validation when the value isnull
.This is not considering the new JSF 2.0
javax.faces.VALIDATE_EMPTY_FIELDS
context parameter which defaults totrue
. It should actually be doing the same asUIInput#validate()
.This has already been reported as Mojarra issue 1508. I've voted it and added a new comment to wake them up.