自定义验证错误无法按预期工作
|
我想验证一个字段,使其只能接受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);
}
}
}
这是我收到不是数字的东西时看到的错误消息:
为什么我看不到消息:只允许输入数字?
没有找到相关结果
已邀请:
1 个回复
呸溉
?
如果默认消息不符合您的需求,您甚至可以定义自己的自定义验证消息。有关更多信息,请参见此答案。 除此之外,您的错误消息是针对
而不是针对
。