Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear h:message or rich:message value in JSF?

Tags:

jsf

richfaces

I am developing a JSF application and I have many user forms where I use JSF validations. I have an issue which annoys me, it will be easier to tell it with an example.

                    <h:panelGroup>
                    <h:selectOneRadio id="gender" value="#{registrationController.person.gender}" 
                                      required="true" requiredMessage="#{msg.commonErrorBlankField}">
                        <f:selectItems value="#{registrationController.genders}" />
                    </h:selectOneRadio>
                    <rich:spacer />
                    <rich:message for="gender" errorLabelClass="errorLabel">
                        <f:facet name="errorMarker">
                            <h:graphicImage value="#{msg.imageExclamation}" />
                        </f:facet>
                    </rich:message>
                </h:panelGroup>

Above if a radio option is not selected a required message is displayed. And when the user makes a selection I see the validation error disseapeares. Fair enough ! My problem is when the user navigates to next page and then by using back button of the browser comes back to this page again I can see my gender field is selected accordingly but validation error is still displayed.

Does anyone know if there is a workaround to clear the h:message field once I click the command button so validation error won't be displayed when I go back to the same page?

like image 370
user355246 Avatar asked Nov 06 '22 12:11

user355246


1 Answers

Not sure if this works. I have not tested it:

//idComponent is the Component whose message you want to clear, e.g. gender
void clearMessageForComponent (final String idComponent) {
        if (idComponent != null) {
            Iterator<FacesMessage> it = FacesContext.getCurrentInstance().getMessages(idComponent);
            while(it.hasNext()){
                ((FacesMessage)it.next()).setDetail("");
            }
        }
    }
like image 111
Averroes Avatar answered Nov 15 '22 12:11

Averroes