停用jsf表单整数验证

时间:2019-02-21 07:46:58

标签: validation jsf

我在jsf中有一个表单,为此我创建了一个特殊的验证器类,该类检查该字段是否为:null,空,有效,数字等。如果输入无效,则该类将引发异常并显示一条消息。问题是,当我在输入字段中输入字母时(输入应为int!),我得到的是默认验证消息,而不是自定义消息。如何禁用默认验证消息?

这是带有默认验证消息的图像:

enter image description here

JSF代码:

<h:form>
            <h:inputText value="#{deleteEmployeeBean.employeeId}"
                id="employeeId" name="employeeId">
                <f:validator validatorId="idValidator" for="employeeId"></f:validator>
            </h:inputText>
            <h:message for="employeeId" style="color: red;"></h:message>
            <h:commandButton action="#{deleteEmployeeBean.loadEmployee}"
                value="#{content.searchButton}"></h:commandButton>
        </h:form>

1 个答案:

答案 0 :(得分:0)

您可以将validatorMessage用于f:validator

<h:inputText value="#{deleteEmployeeBean.employeeId}"
            id="employeeId" name="employeeId" validatorMessage="your message">
            <f:validator validatorId="idValidator" for="employeeId"></f:validator>
 </h:inputText>

或者您可以使用它让用户仅插入数字

    <h:inputText value="#{deleteEmployeeBean.employeeId}"
      id="employeeId" name="employeeId" validatorMessage="your message" 
      onkeypress="if(event.which &lt; 48 || event.which &gt; 57 ) if(event.which != 8) return false;">
       <f:validator validatorId="idValidator" for="employeeId"></f:validator>
   </h:inputText>