开发者

JSF and Ajax validation - How to update message panels for ALL messages?

I want to do validate all input fields with AJAX.

For example, there are 3 input fields. Two of them require minimum length of 3 characters and the other is validated by a simple regex. For <f:ajax> I set up the keyup event to quickly see the ajax validation coming up. A complete example may look like this:

   <h:form>
       <h:messages id="messages" />
       <h:inputText id="text1" value="#{bean.text1}">
           <f:validateLength minimum="3" />
           <f:ajax execute="@this" event="keyup" render="messages" />
       </h:inputText>

       <h:inputText id="text2" value="#{bean.text2}">
           <f:validateLength minimum="3" />
           <f:ajax execute="@this" event="keyup" render="messages" />
       </h:inputText>

        <h:inputText id="text3" value="#{bean.text3}">
            <f:validateRegex pattern="[A-Z][a-zA-Z]*" />
            <f:ajax execute="@this" event="keyup" render="messages" />
        </h:inputText>
    </h:form>

Once I leave the input first input field with an error and jump to the 2nd field and type invalid stuff there, the message from the first invalid fi开发者_高级运维eld disappears, since the messages are rerendered! Setting execute to @form, will cause all fields to be validated, once I start typing within the first input field.

So my question is: How to display ALL validation errors with AJAX and not only the last message from the last invalid field?

P.S.: Im using Mojarra 2.0.3 (+ Richfaces + Primefaces) on Websphere


Not possible. Best what you can do is to just give each input their own message.

<h:form>
    <h:panelGrid columns="2">
        <h:inputText id="text1" value="#{bean.text1}">
            <f:validateLength minimum="3" />
            <f:ajax event="keyup" render="text1message" />
        </h:inputText>
        <h:message id="text1message" for="text1" />

        <h:inputText id="text2" value="#{bean.text2}">
            <f:validateLength minimum="3" />
            <f:ajax event="keyup" render="text2message" />
        </h:inputText>
        <h:message id="text2message" for="text2" />

        <h:inputText id="text3" value="#{bean.text3}">
            <f:validateRegex pattern="[A-Z][a-zA-Z]*" />
            <f:ajax event="keyup" render="text3message" />
        </h:inputText>
        <h:message id="text3message" for="text3" />
    </h:panelGrid>
</h:form>

You can eventually group them together above the form.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜