-1

I am trying to reset the form fields after choosing confirm button in a dialog form

<h:form id="form">  
<p:panel id="panelform">
 <h:panelGrid id="formulaire" columns="2"> 
...
</h:panelGrid>
</p:panel>
 <p:dataTable ..... </p:dataTabe>
</h:form>
<p:confirmDialog style="position: absolute; width: 50px; border-color: blue" id="deleteData"  message="Your Database Will be completely removed. Are you sure? "  
                                     appendToBody="true"                                                               
                                     header="Delete List" severity="alert" widgetVar="deleteDialog">                          
                        <h:form>     
                            <p:commandButton id="confirm" value="Confirm" actionListener="#{MB.deleteData()}" update=":form" ajax="true" oncomplete="deleteDialog.hide(); purchase.hide();" >                            
                            </p:commandButton>  
                            <p:commandButton id="cancel" value="Later" onclick="deleteDialog.hide();" type="button" />                           
                        </h:form>
                    </p:confirmDialog> 

My deleteData method in my sessionScoped bean is

 public String deleteData() {
    logger.log(Level.SEVERE, "*****delete Datas***** ");
    dataBusinessLocal.deletedatas(datas);
    logger.log(Level.SEVERE, "*****delete Data***** ");
    dataBusinessLocal.deleteData(data);
    datas.clear();
    RequestContext.getCurrentInstance().reset("form:panelform");  
    return "datasList";
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
junior developper
  • 448
  • 2
  • 19
  • 40
  • Hi junior developper, what is the actual question - where are the problems you need help for? What have you already tried to solve them? – L-Ray Dec 07 '13 at 10:29
  • @L-Ray: Apparently, the intented reset "didn't work" ;) – BalusC Dec 07 '13 at 10:33
  • @junior developper: Have you checked if your code jumps into the intended method, does it do the intended job there? Have you added a `` - or similar task, that lets you know any errors in validation/update-phase? Does e.g. Firebug show any response? @BalusC: :-) – L-Ray Dec 07 '13 at 10:42
  • @L-Ray no log is displayed in the glassfish log. deleteData() is invoked Becaus the datas was removed and my dataTable was successfully updated But my input fields didn't – junior developper Dec 07 '13 at 10:46
  • 1
    Thnx for the fast response. Have you test-wise tried to reset the fields manually - means, to just put "null" into the setters? Does that work? (So we could figure out, if it is a problem with the `update` or with the fields not being reseted). By the way: `update="@form"` would be less error-prone. – L-Ray Dec 07 '13 at 11:07
  • @L-Ray it works setting the setters top null in the delete method – junior developper Dec 09 '13 at 07:34
  • Could you then please mark my answer as correct and maybe even upvote it? – L-Ray Dec 17 '13 at 11:01

2 Answers2

1

RequestContext.getCurrentInstance().reset("form:panelform"); clears cached values - in case a validation during ValidationPhase did not work out correctly, the wrong values can be shown to the user on the following page.

I guess, what it not does is to clear values from the Model. Caused by this, during RenderResponse the Model values will be written back to the xhtml-output. To prevent that, just set the input fields to null manually in your action method.

The following link "How to clear all input fields in p:dataTable?"suggests setting the input type="reset". If it works, give me a hint. :-)

Community
  • 1
  • 1
L-Ray
  • 1,637
  • 1
  • 16
  • 29
0

This is how you can clear out all child components = reset all their values. This works for submitted forms showing validation errors as well as for newly entered values in a form.

For JSF2 you have to use getFacetsAndChildren() to make your way to Facelets as well.

public static void clearComponent() {
        UIViewRoot root = FacesContext.getCurrentInstance().getViewRoot();

        // for JSF 2 getFacetsAndChildren instead of only JSF 1 getChildren
        Iterator<UIComponent> children = root.getFacetsAndChildren();
        clearAllComponentInChilds(children);
}

private static void clearAllComponentInChilds(Iterator<UIComponent> childrenIt) {

        while(childrenIt.hasNext()) {
            UIComponent component = childrenIt.next();
            log.debug("handling component " + component.getId() );
            if (component instanceof HtmlInputText) {
                HtmlInputText com = (HtmlInputText) component;
                com.resetValue();
            }
            if (component instanceof HtmlSelectOneMenu) {
                HtmlSelectOneMenu com = (HtmlSelectOneMenu) component;
                com.resetValue();
            }
            if (component instanceof HtmlSelectBooleanCheckbox) {
                HtmlSelectBooleanCheckbox com = (HtmlSelectBooleanCheckbox) component;
                com.resetValue();
            }
            if (component instanceof HtmlSelectManyCheckbox) {
                HtmlSelectManyCheckbox com = (HtmlSelectManyCheckbox) component;
                com.resetValue();
            }

            clearAllComponentInChilds(component.getFacetsAndChildren());    

        }

}
jfx
  • 355
  • 1
  • 7