当没有指定值时,是否应该跳过验证?
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从 JSF 2.0 开始,JSF 默认情况下也会验证空字段,以便与新的 Java EE 6 提供的 JSR 303 Bean 验证 API 配合使用,该 API 提供了其他功能
@NotNull
等等。基本上有两种方法可以解决这个问题:
通过
web.xml
中的以下条目告诉 JSF 不要验证空字段。缺点很明显:您无法再充分利用 JSR 303 Bean Validation。
在
验证器
中自行进行空检查。如果您没有设置为
true
的javax.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:
Tell JSF to not validate empty fields by the following entry in
web.xml
.The disadvantage is obvious: you can't utilize JSR 303 Bean Validation at its full powers anymore.
Do a nullcheck yourself in the
Validator
.If you don't have a
javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL
context param which is set totrue
, then you'd like to cast toString
and checkisEmpty()
as well.