没有指定值时,不应该跳过验证吗? [英] Shouldn't the validation be skipped when there is no value specified?
问题描述
我正在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);
}
}
}
从JSF 2.0开始,默认情况下,JSF还将验证空字段,以便与新的Java EE 6提供的JSR 303 Bean验证API很好地配合使用.其中包括 @NotNull
等. /p>
基本上有两种方法可以解决此问题:
-
通过
web.xml
中的以下条目告知JSF不验证空字段.<context-param> <param-name>javax.faces.VALIDATE_EMPTY_FIELDS</param-name> <param-value>false</param-value> </context-param>
缺点很明显:您无法再充分利用JSR 303 Bean验证了.
-
在
Validator
中进行空检查.if (object == null) return;
如果没有设置为
true
的javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL
上下文参数,那么您想转换为String
并同时检查isEmpty()
.
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);
}
}
}
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
.<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.
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 totrue
, then you'd like to cast toString
and checkisEmpty()
as well.
这篇关于没有指定值时,不应该跳过验证吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!