提交的输入值为null时,不调用JSF定制验证器 [英] JSF custom validator not invoked when submitted input value is null
问题描述
我创建了一个自定义验证器,当输入为null时不会被调用.
I created a custom validator which is not getting called when input is null.
我的验证者代码:
@FacesValidator("requiredValidator")
public class RequredValidation implements Validator {
public void validate(FacesContext context, UIComponent component,
Object value) throws ValidatorException {
System.out.println("in valid GGGGGGGG");
}
我的xhtml页面代码:
My xhtml page code:
<p:message for="urlInput" />
<p:inputText id="urlInputv" value="#{coverageBean.firstname}" label="URL" maxlength="2" >
<f:validator validatorId="requiredValidator"></f:validator>
</p:inputText>
<p:message for="urlInputv" />
<p:commandButton value="submit" action="#{loginBean.validateText}" />
现在,当我在文本框中输入任何值时,此方法有效,但当inputtext为null时,此方法无效.
Now this is working when I am entering any value in text box, but it is not working when inputtext is null.
服务器正在使用
- Tomcat
- primefaces 3.5
- jsf2.0
请问任何人有什么问题吗?
Please could anyone tell what the problem is?
推荐答案
默认情况下,如果Bean验证(JSR303)在环境中不可用,则JSF不会验证空的提交值.可以使用上下文参数javax.faces.VALIDATE_EMPTY_FIELDS
来控制此行为.
By default, JSF does not validate an empty submitted value if Bean Validation (JSR303) is not available in the environment. This behavior can be controlled with the context parameter javax.faces.VALIDATE_EMPTY_FIELDS
.
javax.faces.VALIDATE_EMPTY_FIELDS
的默认值为auto
,这意味着如果类路径中有Bean验证(JSR303)可用,则空字段将得到验证.
The default value for javax.faces.VALIDATE_EMPTY_FIELDS
is auto
, meaning that empty fields are validated if Bean Validation (JSR303) is available in the class path.
如果无论如何都想在没有Bean验证的情况下验证空字段,则可以像下面这样在web.xml
中将context参数显式设置为true
:
If you want to validate empty fields anyway without Bean Validation, then explicitly set the context parameter in your web.xml
to true
like this:
<context-param>
<param-name>javax.faces.VALIDATE_EMPTY_FIELDS</param-name>
<param-value>true</param-value>
</context-param>
必须说,如果要根据需要验证输入字段,通常使用required="true"
.然后,您无需在自定义验证器中执行该工作.
It must be said that you normally use required="true"
in case you want to validate an input field as required. You don't need to perform that job in your custom validator then.
<p:inputText ... required="true" requiredMessage="Please enter value" />
或者,用<f:validateRequired>
更抽象:
<p:inputText ... requiredMessage="Please enter value">
<f:validateRequired />
</p:inputText>
请注意,您可以在同一输入组件上安全地使用多个验证器.
Do note that you can safely use multiple validators on the very same input component.
这篇关于提交的输入值为null时,不调用JSF定制验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!