Bean值未在jsf中的验证中更新

时间:2013-12-10 14:24:31

标签: jsf

我有一个场景,我有一个表格,包括手机,家庭电话和办公室电话。每个字段都有一个名为首选行的相应单选按钮。只能选择一个或三个无线电。当单独选择时,应该要求该字段。所以我把代码框如下。

<table class="addaccount-table" cellpadding="0" cellspacing="0"
border="0">
<tr>
    <td><label><h:outputText value="Home Phone" /></label></td>
    <td><h:inputText id="homePhone" maxlength="14"
            value="#{Controller.ContactPageBean.homePhone}"
            required="#{not empty Controller.ContactPageBean.homePhoneSelected}"
            requiredMessage="Preferred phone is required">
        </h:inputText> <br /> <h:message for="homePhone" style="color:red"></h:message></td>
    <td><h:selectOneRadio id="homePhoneSelectedId"
            styleClass="addacountradio"
            value="#{Controller.ContactPageBean.homePhoneSelected}">
            <f:selectItem itemValue="Home" itemLabel="Preferred" />
            <f:ajax render="homePhone" />
        </h:selectOneRadio></td>
</tr>
<tr>
    <td><label><h:outputText value="Office Phone" /></label></td>
    <td><h:inputText id="officePhone" maxlength="14"
            value="#{Controller.ContactPageBean.officePhone}"
            required="#{not empty Controller.ContactPageBean.officePhoneSelected}"
            requiredMessage="Preferred phone is required">
        </h:inputText> <br /> <h:message for="homePhone" style="color:red"></h:message></td>
    <td><h:selectOneRadio id="officePhoneSelectedId"
            styleClass="addacountradio"
            value="#{Controller.ContactPageBean.officePhoneSelected}">
            <f:selectItem itemValue="Home" itemLabel="Preferred" />
            <f:ajax render="officePhone" />
        </h:selectOneRadio></td>
</tr>
<tr>
    <td><label><h:outputText value="Mobile Phone" /></label></td>
    <td><h:inputText id="mobilePhone" maxlength="14"
            value="#{Controller.ContactPageBean.mobilePhone}"
            required="#{not empty Controller.ContactPageBean.mobilePhoneSelected}"
            requiredMessage="Preferred phone is required">
        </h:inputText> <br /> <h:message for="mobilePhone" style="color:red"></h:message></td>
    <td><h:selectOneRadio id="mobilePhoneSelectedId"
            styleClass="addacountradio"
            value="#{Controller.ContactPageBean.mobilePhoneSelected}">
            <f:selectItem itemValue="Home" itemLabel="Preferred" />
            <f:ajax render="mobilePhone" />
        </h:selectOneRadio></td>
</tr>

现在,当我点击行中的三个单选按钮并提交我收到三个字段的必需错误消息。我只期望当前选择单选按钮的错误消息。请帮忙。

1 个答案:

答案 0 :(得分:0)

您将officePhone的h:message错误输入为<h:message for="homePhone" style="color:red">,而它应为<h:message for="officePhone" style="color:red">

另外,我建议您编辑除家庭电话以外的所有<f:selectItem itemValue="Home" itemLabel="Preferred" />。例如<f:selectItem itemValue="Office" itemLabel="Preferred" />

我尝试了这一切,一切正常,请查看:

<h:form>
    <table class="addaccount-table" cellpadding="0" cellspacing="0"
           border="0">
        <tr>
            <td>
                <label><h:outputText value="Home Phone" /></label>
            </td>
            <td><h:inputText id="homePhone" maxlength="14"
                             value="#{ContactPageBean.homePhone}"
                             required="#{not empty ContactPageBean.homePhoneSelected}"
                             requiredMessage="Home phone is required">
                </h:inputText> <br /> 
                <h:message for="homePhone" style="color:red"></h:message>
            </td>
            <td>
                <h:selectOneRadio id="homePhoneSelectedId"
                                  styleClass="addacountradio"
                                  value="#{ContactPageBean.homePhoneSelected}">
                    <f:selectItem itemValue="Home" itemLabel="Preferred" />
                    <f:ajax render="homePhone" />
                </h:selectOneRadio>
            </td>
        </tr>
        <tr>
            <td>
                <label><h:outputText value="Office Phone" /></label>
            </td>
            <td>
                <h:inputText id="officePhone" maxlength="14"
                             value="#{ContactPageBean.officePhone}"
                             required="#{not empty ContactPageBean.officePhoneSelected}"
                             requiredMessage="Office phone is required">
                </h:inputText> <br /> 
                <h:message for="officePhone" style="color:red"></h:message>
            </td>
            <td>
                <h:selectOneRadio id="officePhoneSelectedId"
                                  styleClass="addacountradio"
                                  value="#{ContactPageBean.officePhoneSelected}">
                    <f:selectItem itemValue="Office" itemLabel="Preferred" />
                    <f:ajax render="officePhone" />
                </h:selectOneRadio>
            </td>
        </tr>
        <tr>
            <td>
                <label><h:outputText value="Mobile Phone" /></label>
            </td>
            <td>
                <h:inputText id="mobilePhone" maxlength="14"
                             value="#{ContactPageBean.mobilePhone}"
                             required="#{not empty ContactPageBean.mobilePhoneSelected}"
                             requiredMessage="Mobile phone is required">
                </h:inputText> <br /> 
                <h:message for="mobilePhone" style="color:red"></h:message>
            </td>
            <td>
                <h:selectOneRadio id="mobilePhoneSelectedId"
                                  styleClass="addacountradio"
                                  value="#{ContactPageBean.mobilePhoneSelected}">
                    <f:selectItem itemValue="Mobile" itemLabel="Preferred" />
                    <f:ajax render="mobilePhone" />
                </h:selectOneRadio>
            </td>
        </tr>
    </table>
        <h:commandButton value="submit" action="#{ContactPageBean.doSome()}"/>
        </h:form>

为简单起见,我将Controller.ContactPageBean更改为ContactPageBean 最后,也许最好使用复选框而不是radioButton,不是吗?