JSF 2.0使用JS或AJAX清除验证错误消息



我有一个带有两个单选按钮和两个相应文本框的网页。两者都是一个无线电组的一部分。该页面还有一个提交按钮。

o Radio1[文本框1]o Radio2[Textbox2]

|提交|

当我选择Radio1并单击提交时,如果出现错误,将显示验证错误消息。这些是gloable错误消息,显示在所有字段的页面顶部。我使用了的validatorMessage属性

<h:inputText> 

显示错误和

<f:validateRegex pattern="(^[0-9]{1,15}$)" /> 

以验证错误。我还有

<h:messages class="errors"  style="color:red;" layout="table" />

在xhtml页面的顶部。

我的要求是,当我点击Radio2时,我需要Radio1的错误消息消失,因为它们与Radio2无关。反之亦然。

有没有一种方法可以在选择Radio2时使用AJax或JS清除Radio1的错误消息?

更新无线电代码:

<t:selectOneRadio id="options" value="#{reqscope.selectedRadio}" layout="spread">
    <f:selectItem itemValue="radio1" itemLabel="Policy number" />
    <f:selectItem itemValue="radio2" itemLabel="Customer ID" />    
</t:selectOneRadio>
<t:radio for="options" index="0"  name="search_by1" onclick="searchSelected()">

您可以使用<f:ajax execute>仅指定那些需要处理(从而转换和验证)的字段。您可以使用<f:ajax render>来指定需要ajax更新的组件,在本例中是消息组件,以便用新消息刷新它。

在文本框的<f:validateRegex>上,只要选定的单选按钮值与预期值不匹配,就可以使用disabled属性禁用验证。

下面的例子应该做:

<h:messages id="messages" ... />
<h:form id="form">
    ...
    <t:selectOneRadio id="options" ...>
        ...
        <f:ajax execute="@this textbox1 textbox2" render=":messages" />
    </t:selectOneRadio>
    ...
    <h:inputText id="textbox1" ...>
        <f:validateRegex ... disabled="#{param['form:options'] != 'radio1'}" />
    </h:inputText>
    <h:inputText id="textbox2" ...>
        <f:validateRegex ... disabled="#{param['form:options'] != 'radio2'}" />
    </h:inputText>
    ...
</h:form>

使用Prime Faces可以使用

您可以在以下链接中看到一个示例http://www.primefaces.org/showcase/ui/misc/resetInput.xhtml

最新更新