9 Replies Latest reply on Aug 26, 2009 6:20 AM by dmitri.ilyin

    Help! Very BASIC JSF question!!

      Hi all,

      i have to implement very basic and simple (i think) pattern. Regardless with or without RichFaces. I hope there is much of competence for JSF here in forum :)

      I have a form with a couple of input fields in it. The value for the field are comming from a bean. The property of the bean have initial values. So when the form first time shown the input fields are initialized. The fields have validators. In the form a have two commands. The one is "OK" and calls some action and another is "reset". The "reset" command must "restore" the initial values (comes from the bean properties) for the input fields. Thats ALL!

      So, the thing is about 10 lines of code. Very simple i thought. After valildation ("OK" command) is failed, the form (view) will be redisplayed and the input fields on the form still have entered before values. Thats ok. But now i want to reset the values in the input fields with "reset" command. And i don't know HOW. The entered values stay sticky in the fields (as local values in the UIInput components, i think) after validation was failed.

      Two solutions comes in to the head for me:
      - direct call setValue() on UIInput component. It is bad, couse i would have to reset each input field manually each time but i need a common solutions, not only for one case.
      - "restart" the view via JSF navigation. Will work may be, but a have to reset only part of the view (modal Panel).

      I hoped that A4J reRender will does it, but it does not. :(

      I think it could be a basic JSF/RichFaces question. Since the pattern is not very uncommon.

      thanks for any advises.

      regards
      Dmitri

        • 1. Re: Help! Very BASIC JSF question!!
          timgozag

          It is not simple as you thought. a4j reRender will not work because it is only rerender the form in your modal panel, but values in your bean are still there.
          There are two options I think:
          1. Easy way: have a cancel button instead of a reset button in your modal. When click on Cancel (use immediate=true to pass the validation), it will trigger a cancel function in your backing bean then clear out all the UIComponent of that form (get it from FaceContext). So when users open the modal Panel again, you just reRender the modal's form and all your initial values will be there.
          2. Keep the reset button but you have to clear all the component's value then reset them.

          • 2. Re: Help! Very BASIC JSF question!!

            The explicit clear the values is exactly what i wanted to avoid.
            I have several modal dialogs with diferent input fields of diferent types and ids. So i would have to make this "manual clear" each time.

            • 3. Re: Help! Very BASIC JSF question!!

              Hi,

              I have faced a similar kind of problem in my earlier project.


              What I did,

              When I click the reset button, I disabled all the fields through javascript and submit the page to an empty method. When i come back, everything is clear.
              :))

              Though just a hack, it works and is less tedious.

              Akash.
              thedaedals.com/

              • 4. Re: Help! Very BASIC JSF question!!

                 

                "asingla4" wrote:
                Hi,

                I have faced a similar kind of problem in my earlier project.


                What I did,

                When I click the reset button, I disabled all the fields through javascript and submit the page to an empty method. When i come back, everything is clear.
                :))

                Though just a hack, it works and is less tedious.

                Akash.
                http://www.thedaedals.com/


                • 5. Re: Help! Very BASIC JSF question!!
                  ilya_shaikovsky
                  • 6. Re: Help! Very BASIC JSF question!!

                    thanks for the tips.

                    @asingla4: Could you provide you JavaScript code pls? Is it generic or you have hard coded you input field in it?


                    The Ilya's approach seems to be a less hack for me. :) BUT it does not solve my problem completely. Becouse avoiding submmited values don't clear the already set local values.

                    My scenario:

                    i have two input fields -> i fill both of them -> first field input will be validated and validation fails -> second field gets locaValue -> now i call method with a4j:region button, to avoid submiting any values in the form -> first field gets value from EL (fine) -> second field shows localValue (it was inialized in previous call).

                    It seems to me the only solution is to reset the local values manualy.
                    What i would need is a TAG just like <a4j:region>, that will reset local values of all input fields found within it before render response phase. It could be generic enough for my scenario...

                    • 7. Re: Help! Very BASIC JSF question!!
                      nbelaevski

                      Hi,

                      Please consider this example:

                      <h:form>
                       <rich:messages />
                      
                       <a4j:outputPanel id="box">
                       <h:inputText value="#{forum5Bean.text}">
                       <f:validateLength minimum="3" />
                       </h:inputText>
                      
                       <h:inputText value="#{forum5Bean.text2}">
                       <f:validateLength minimum="3" />
                       </h:inputText>
                      
                       <a4j:commandLink value="Apply" /> 
                       <a4j:commandLink value="Reset" ajaxSingle="true" reRender="box" actionListener="#{forum5Bean.resetValues}" />
                       </a4j:outputPanel>
                      </h:form>
                      


                      public void resetValues(ActionEvent actionEvent) {
                       UIComponent component = actionEvent.getComponent();
                       Iterator<UIComponent> children = component.getParent().getChildren().iterator();
                       while (children.hasNext()) {
                       UIComponent child = children.next();
                       if (child != component) {
                       children.remove();
                       }
                       }
                      
                       this.text = "text";
                       this.text2 = "text2";
                       }


                      The idea behind this is that you can remove just a subtree of component tree - view handler will build it for you then, but without any saved values.


                      • 8. Re: Help! Very BASIC JSF question!!
                        m.a.g
                        • 9. Re: Help! Very BASIC JSF question!!

                          Hi,

                          i think the nick's solution is mostly generic. I will use it.

                          thanks a lot!