当没有指定值时,是否应该跳过验证?

发布于 2024-11-09 14:45:31 字数 1582 浏览 0 评论 0原文

我在 GlassFish 3 上使用 JSF2。

我有一个接受可选电话号码的表单。我有这个自定义电话号码验证器(如下),并且我将该字段设置为 required="false",因为电话号码在表单中是可选的。

问题是,该领域的价值总是得到验证。当没有指定值时,是否应该跳过验证?

一定是我做错了什么。任何帮助表示赞赏,谢谢!

<h:outputText value="#{msg.profile_otherPhone1Label}#{msg.colon}" />
  <h:panelGroup>
    <p:inputText label="#{msg.profile_otherPhone1Label}" id="otherPhone1" value="#{profileHandler.profileBean.otherPhone1}" required="false">
      <f:validator validatorId="phoneValidator" />
    </p:inputText>
  <p:spacer width="12"/>
  <h:outputText value="#{msg.profile_phoneExample}" />
</h:panelGroup>

#

public class PhoneValidator implements Validator {

    @Override
    public void validate(FacesContext facesContext, UIComponent uIComponent,
            Object object) throws ValidatorException {

        String phone = (String) object;

        // count the digits; must have at least 9 digits to be a valid phone number
        // note: we're not enforcing the format because there can be a lot of variation
        int iDigitCount = 0;
        for (char c : phone.toCharArray()) {
            if (Character.isDigit(c)) {
                iDigitCount++;
            }
        }

        if (iDigitCount < 9) {
            FacesMessage message = new FacesMessage();
            message.setSummary(Messages.getMessage("error_phoneNumberNotValid"));
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(message);
        }
    }

}

I'm using JSF2 on GlassFish 3.

I have a form that accepts and optional phone number. I have this custom phone number validator (below), and I have the field set to required="false" because the phone number is optional in the form.

The problem is, the value in the field is always getting validated. Shouldn't the validation be skipped when there is no value specified?

There must be something I'm doing wrong. Any help is appreciated, thanks!

<h:outputText value="#{msg.profile_otherPhone1Label}#{msg.colon}" />
  <h:panelGroup>
    <p:inputText label="#{msg.profile_otherPhone1Label}" id="otherPhone1" value="#{profileHandler.profileBean.otherPhone1}" required="false">
      <f:validator validatorId="phoneValidator" />
    </p:inputText>
  <p:spacer width="12"/>
  <h:outputText value="#{msg.profile_phoneExample}" />
</h:panelGroup>

#

public class PhoneValidator implements Validator {

    @Override
    public void validate(FacesContext facesContext, UIComponent uIComponent,
            Object object) throws ValidatorException {

        String phone = (String) object;

        // count the digits; must have at least 9 digits to be a valid phone number
        // note: we're not enforcing the format because there can be a lot of variation
        int iDigitCount = 0;
        for (char c : phone.toCharArray()) {
            if (Character.isDigit(c)) {
                iDigitCount++;
            }
        }

        if (iDigitCount < 9) {
            FacesMessage message = new FacesMessage();
            message.setSummary(Messages.getMessage("error_phoneNumberNotValid"));
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(message);
        }
    }

}

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

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

发布评论

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

评论(1

一个人的夜不怕黑 2024-11-16 14:45:31

从 JSF 2.0 开始,JSF 默认情况下也会验证空字段,以便与新的 Java EE 6 提供的 JSR 303 Bean 验证 API 配合使用,该 API 提供了其他功能 @NotNull 等等。

基本上有两种方法可以解决这个问题:

  1. 通过 web.xml 中的以下条目告诉 JSF 不要验证空字段。

    <上下文参数>
        <参数名称>javax.faces.VALIDATE_EMPTY_FIELDS
        <参数值> false
    
    

    缺点很明显:您无法再充分利用 JSR 303 Bean Validation。


  2. 验证器中自行进行空检查。

    if (object == null) return;
    

    如果您没有设置为 truejavax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL 上下文参数,那么您需要转换为 String 并检查 isEmpty()

From JSF 2.0 on, JSF will by default validate empty fields as well in order to play well with the new Java EE 6 provided JSR 303 Bean Validation API which offers among others @NotNull and on.

There are basically two ways to go around this:

  1. Tell JSF to not validate empty fields by the following entry in web.xml.

    <context-param>
        <param-name>javax.faces.VALIDATE_EMPTY_FIELDS</param-name>
        <param-value>false</param-value>
    </context-param>
    

    The disadvantage is obvious: you can't utilize JSR 303 Bean Validation at its full powers anymore.


  2. Do a nullcheck yourself in the Validator.

    if (object == null) return;
    

    If you don't have a javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL context param which is set to true, then you'd like to cast to String and check isEmpty() as well.

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