Closing popup only if the form is valid

692 Views Asked by At

I want to validate my inputText field, which is in a popupPanel. It should contains only numbers.

<h:form>
    <h:outputText value="Input:"/>
        <h:inputText id="myID" value="#{myBean.field}"
            validatorMessage="Only numbers">
            <f:validateRegex pattern="([0-9])*$" />
            <rich:validator />
            <a4j:ajax event="change" render="msgValidator" />
        </h:inputText>
        <h:message id="msgValidator" for="myID" styleClass="text_colorRed" />
</h:form>

After all I want to save all with button. If the input is correct I want to close the popup, otherwise I want to re-insert the correct input without closing popup.

<a4j:ajaxButton type="submit" value="Save" styleClass="text_weigthBold"
    action="#{myBean.save()}" render="myTable"
    oncomplete="#{rich:component('myPopup')}.hide();" execute="@this">
</a4j:ajaxButton>

Unfortunately when I type wrong input and click two times on the button, it save the request and close the popup without requesting to input the correct text.

I also used a Java validator but the behavior is still the same.

What can I do to correct this bug?

2

There are 2 best solutions below

6
On

This is because you're closing popup unconditionally. You must check yourself if validation was OK. In JSF 2.0 you can use FacesContext#isValidationFailed().

<a4j:commandButton execute="@form" value="Save" styleClass="text_weigthBold" 
    oncomplete="if (!#{facesContext.validationFailed}) {#{rich:component('myPopup')}.hide();}"
    action="#{myBean.save}" render="myTable" />

To check also other errors you can use facesContext.maximumSeverity.ordinal gt 0 or facesContext.maximumSeverity != null.

I don't know RF 3.X (and you didn't tell if you're using RF 3 or 4) but it looks like it never had component named a4j:ajaxButton, did you mean a4j:commandButton? Also note that I've changed action="#{myBean.save()}" to action="#{myBean.save}" which is corrent.

0
On

Resolved. In the form there were some h:checkbox, I remove the checkboxes and now it works fine. I dont know why the Richfaces checkbox doesn't work correctly but I replace them with HTML checkbox.