自定义验证错误未按预期工作

发布于 2024-11-04 08:28:09 字数 1586 浏览 0 评论 0原文

我想验证一个字段只能接受 1 到 100 之间的值。它工作正常,但是当我写一个不是整数的东西时,看不到我期望的自定义​​消息。

这是字段:

<h:inputText id="discountPercentage" value="#{newOfferSupportController.discountPercentage}" validator="#{newOfferSupportController.validateDiscountPercentage}"/>
            <span style="color: red;"><h:message for="discountPercentage"
                showDetail="true" /></span>

这是验证器方法:

public void validateDiscountPercentage(FacesContext context,
            UIComponent validate, Object value) {
        FacesMessage msg = new FacesMessage("");
        String inputFromField = "" + value.toString();
        String simpleTextPatternText = "^([1-9]|[1-9]\\d|100)$";
        Pattern textPattern = null;
        Matcher productValueMatcher = null;
        textPattern = Pattern.compile(simpleTextPatternText);
        productValueMatcher = textPattern.matcher(inputFromField);

        if (!productValueMatcher.matches()) {
            msg = new FacesMessage("Only values between 1 and 100 allowed");
            throw new ValidatorException(msg);
        }

        for (int i = 0; i < inputFromField.length(); i++) {
            // If we find a non-digit character throw Exception
            if (!Character.isDigit(inputFromField.charAt(i))) {
                msg = new FacesMessage("Only numbers allowed");
                throw new ValidatorException(msg);
            }
        }
    }

这是当我酯化非数字的内容时看到的错误消息:

“在此处输入图像描述”

为什么我看不到消息:只允许输入数字?

I want to validate a field to be able to accept values only between 1 and 100. It works fine, but when i write a something that is not an integer is don't see the custom message i expect.

This is the field:

<h:inputText id="discountPercentage" value="#{newOfferSupportController.discountPercentage}" validator="#{newOfferSupportController.validateDiscountPercentage}"/>
            <span style="color: red;"><h:message for="discountPercentage"
                showDetail="true" /></span>

This is the validator method:

public void validateDiscountPercentage(FacesContext context,
            UIComponent validate, Object value) {
        FacesMessage msg = new FacesMessage("");
        String inputFromField = "" + value.toString();
        String simpleTextPatternText = "^([1-9]|[1-9]\\d|100)
quot;;
        Pattern textPattern = null;
        Matcher productValueMatcher = null;
        textPattern = Pattern.compile(simpleTextPatternText);
        productValueMatcher = textPattern.matcher(inputFromField);

        if (!productValueMatcher.matches()) {
            msg = new FacesMessage("Only values between 1 and 100 allowed");
            throw new ValidatorException(msg);
        }

        for (int i = 0; i < inputFromField.length(); i++) {
            // If we find a non-digit character throw Exception
            if (!Character.isDigit(inputFromField.charAt(i))) {
                msg = new FacesMessage("Only numbers allowed");
                throw new ValidatorException(msg);
            }
        }
    }

This is the error i message i see when i ester something that is not a number:

enter image description here

Why i don't see the message: Only numbers allowed?

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

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

发布评论

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

评论(1

驱逐舰岛风号 2024-11-11 08:28:09

为什么不使用jsf的 LongRangeValidator 为此目的?

<h:inputText id="discountPercentage" 
      value="#{newOfferSupportController.discountPercentage}">
   <f:validateLongRange minimum="1" maximum="100"/>
</h:inputText>

如果默认消息不符合您的需求,您甚至可以定义自己的自定义验证消息。有关详细信息,请参阅此答案

除此之外,您的错误消息是针对 productValue 而不是针对 discountPercentage

Why don't you use jsf's LongRangeValidator for this purpose?

<h:inputText id="discountPercentage" 
      value="#{newOfferSupportController.discountPercentage}">
   <f:validateLongRange minimum="1" maximum="100"/>
</h:inputText>

You can even define your own custom validation messages if the default messages don't fit your needs. See this answer for more information.

Besides this, your error message is for productValue and not for discountPercentage.

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