Validator is called but error message is not displayed

牧云@^-^@ 提交于 2019-12-11 11:04:04

问题


when i click on the command button. validate method is getting called but the error message is not getting displayed..

here is my code..

<h:form id="form">
    <h:body>
        <p:panel style="width:500px">
            <h:outputLabel for="year" value="Select Year: *" style="font-weight:bold" />
            <p:selectOneMenu id="year" value="#{leaveBean.year}">
                <f:selectItem itemLabel="Select One" itemValue="null" />
                <f:selectItems value="#{leaveBean.yearDTO}" var="currentUser" itemValue="#{currentUser.name}" itemLabel="#{currentUser.name}" />
                <f:validator validatorId="LeaveCardValidator" />
            </p:selectOneMenu>
        </p:panel>

        <p:commandButton value="Submit" action="#{leaveController.leaveCard}" update="updateList,updateDetails" id="button"/>
       <h:message for="year" style="color:red"/>

回答1:


You seem to expect that JSF auto-updates the <h:message> on every ajax request. This is untrue. Perhaps you're confusing with PrimeFaces <p:messages> or <p:growl> which have each an autoUpdate attribute which enables you to tell them to auto-update themselves on every ajax request.

You really need to make sure that the <h:message> is covered by the ajax update. Just give it an ID

<h:message id="yearMessage" ... />

and include it in the client ID collection of the ajax update

<p:commandButton ... update="updateList updateDetails yearMessage" />

An alternative would be to replace <h:message> by <p:messages autoUpdate="true">.




回答2:


Not sure where are the updateList and updateDetails are located but in the example give above you should use update="@form" instead or in addtion like this:

update="updateList updateDetails @form" 

so that the form will be rendered again...




回答3:


just use one of these : update the whole form in order to update the content of <h:message />

<p:commandButton value="Submit" action="#{leaveController.leaveCard}" update="@form" id="button"/>

or give the <h:message /> an id and id this id to the <p:commandButton/>
<h:message id="msg" for="year" style="color:red"/>

<p:commandButton value="Submit" action="#{leaveController.leaveCard}" update="updateList,updateDetails,msg" id="button"/>


来源:https://stackoverflow.com/questions/18585042/validator-is-called-but-error-message-is-not-displayed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!