使用内联验证方法和验证器类验证空字段

发布于 2025-01-08 15:50:59 字数 504 浏览 4 评论 0 原文

我有以下输入字段:

<h:inputText value=".." validator="#{labController.validateValue}"/>

如果该字段为空,则不会对其进行验证(不会调用 labController 中的 validateValue)。

但是使用单独的验证器类:

<h:inputText value=".." >
  <f:validator validatorId="labDateValidator"/>
</h:inputText>

那么即使输入字段为空,也可以调用它的 validate 方法吗?

这是我观察到的。此行为是否取决于实现或版本(我使用的是 Mojarra 2.1)?

背景是我想使用自己的方法/类来完成所有验证任务,包括所需的验证。它仅适用于验证器类吗?

I have the following input field:

<h:inputText value=".." validator="#{labController.validateValue}"/>

If the field is empty it will not be validated (validateValue in labController is not called).

But using a separate validator class:

<h:inputText value=".." >
  <f:validator validatorId="labDateValidator"/>
</h:inputText>

then its validate method well be called even with an empty input field?

This is what I observe. Is this behavior dependent on implementation or version (I am using Mojarra 2.1) ?

Background is that I want to use my own method/class for all validation tasks including required validation. Does it work with validator class only?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可是我不能没有你 2025-01-15 15:50:59

您在 JSF 中遇到了疏忽。 validator 封装在 MethodExpressionValidator< /code> 根据其validate() 方法确实在值为 null 时跳过验证。

if (value != null) {
    try {
        ELContext elContext = context.getELContext();
        methodExpression.invoke(elContext, new Object[]{context, component, value});
        ...

这没有考虑新的 JSF 2.0 javax.faces.VALIDATE_EMPTY_FIELDS 上下文参数,该参数默认为 true。它实际上应该与 UIInput#validate()

if (!isEmpty(newValue) || validateEmptyFields(context)) {
    try {
        ELContext elContext = context.getELContext();
        methodExpression.invoke(elContext, new Object[]{context, component, value});
        ...

这已被报告为 Mojarra 问题 1508。我已经投票并添加了新评论来唤醒他们。

You've hit an oversight in JSF. The validator is wrapped in a MethodExpressionValidator which according to the source of its validate() method indeed skips validation when the value is null.

if (value != null) {
    try {
        ELContext elContext = context.getELContext();
        methodExpression.invoke(elContext, new Object[]{context, component, value});
        ...

This is not considering the new JSF 2.0 javax.faces.VALIDATE_EMPTY_FIELDS context parameter which defaults to true. It should actually be doing the same as UIInput#validate().

if (!isEmpty(newValue) || validateEmptyFields(context)) {
    try {
        ELContext elContext = context.getELContext();
        methodExpression.invoke(elContext, new Object[]{context, component, value});
        ...

This has already been reported as Mojarra issue 1508. I've voted it and added a new comment to wake them up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文