提交的输入值为空时不调用 JSF 自定义验证器 [英] JSF custom validator not invoked when submitted input value is null

查看:16
本文介绍了提交的输入值为空时不调用 JSF 自定义验证器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义验证器,当输入为空时它不会被调用.

我的验证器代码:

@FacesValidator("requiredValidator")公共类 RequredValidation 实现了 Validator {公共无效验证(FacesContext 上下文,UIComponent 组件,对象值)抛出 ValidatorException {System.out.println("在有效的GGGGGGGG中");}

我的 xhtml 页面代码:

<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 时,它不起作用.

服务器正在使用

  • 雄猫
  • 素数 3.5
  • jsf2.0

谁能告诉我是什么问题?

解决方案

默认情况下,如果 Bean Validation (JSR303) 在环境中不可用,JSF 不会验证空提交的值.可以使用上下文参数 javax.faces.VALIDATE_EMPTY_FIELDS 控制此行为.

javax.faces.VALIDATE_EMPTY_FIELDS 的默认值是 auto,这意味着如果 Bean Validation (JSR303) 在类路径中可用,则验证空字段.>

如果您想在没有 Bean Validation 的情况下仍然验证空字段,则将 web.xml 中的上下文参数显式设置为 true,如下所示:

<param-name>javax.faces.VALIDATE_EMPTY_FIELDS</param-name><param-value>true</param-value></context-param>

必须指出的是,如果您想根据需要验证输入字段,通常会使用 required="true".然后,您无需在自定义验证器中执行该作业.

<p:inputText ... required="true" requiredMessage="请输入值"/>

或者,使用 更抽象:

<f:validateRequired/></p:inputText>

请注意,您可以在同一个输入组件上安全地使用多个验证器.

I created a custom validator which is not getting called when input is null.

My validator code:

@FacesValidator("requiredValidator")
public class RequredValidation implements Validator {

    public void validate(FacesContext context, UIComponent component,
            Object value) throws ValidatorException {
        System.out.println("in valid GGGGGGGG");

    }

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}" />

Now this is working when I am entering any value in text box, but it is not working when inputtext is null.

The server is using

  • Tomcat
  • primefaces 3.5
  • jsf2.0

Please could anyone tell what the problem is?

解决方案

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.

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.

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>

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" />

Or, more abstract with <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.

这篇关于提交的输入值为空时不调用 JSF 自定义验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆