7 Replies Latest reply on Aug 31, 2007 2:02 PM by ssilvert

    Submitting form without submit button

    pieter.kuijpers

      I have a page containing a form, but no submit button. Form submit is done through JavaScript on the onChange of a field in the form. There are also other forms on the page.

      ClientFacade currently has only the submit(String componentID) method for submitting a form, where componentID is supposed to be the ID of a submit button. Since I have no submit button, this method does not work for me. I would like to be able to use submit(componentID) with an arbitrary component ID, where the form containing that component is submitted.

      The following patch to ClientFacade makes this possible:

      @@ -240,12 +240,13 @@
       }
      
       /**
      - * Finds the named submit button on a form and submits the form.
      + * Finds the named component on a form and submits the form. If the named component is a
      + * submit button, this button will be "pressed" to submit the form.
       *
       * At the end of this method call, the new view will be loaded so you can
       * perform tests on the next page.
       *
      - * @param componentID The JSF component id (or a suffix of the client ID) of the submit button to be "pressed".
      + * @param componentID The JSF component id (or a suffix of the client ID)
       *
       * @throws IOException if there is a problem submitting the form.
       * @throws SAXException if the response page can not be parsed
      @@ -257,7 +258,11 @@
       String clientID = this.clientIDs.findClientID(componentID);
       WebForm form = getForm(clientID);
       SubmitButton button = form.getSubmitButtonWithID(clientID);
      - this.webResponse = form.submit(button);
      + if (button != null) {
      + this.webResponse = form.submit(button);
      + } else {
      + this.webResponse = form.submit();
      + }
       this.clientIDs = new ClientIDs();
       }
      


      What do you think? Is this a useful addition to JSFUnit?

      Regards,
      Pieter Kuijpers

        • 1. Re: Submitting form without submit button
          ssilvert

          Yes, I think that's very useful.

          I'd rather not overload the meaning of submit(componentID). I'm becoming more and more aware of how important it is to make the meaning of these methods as simple and clear as possible. So I'd rather have a new method for this. The question is what to call it. I'd like something that conveys the meaning. All I can come up with right now is nakedSubmit().

          What are your thoughts?

          Stan

          • 2. Re: Submitting form without submit button
            ssilvert

            I just took a look at the HttpUnit javadoc. WebForm has a method called submitNoButton(). Maybe that's what we should call the new method. Also, it looks like that does a submit and ignores any buttons where the plain submit() does not.

            So maybe we should name the method submitNoButtion() and also call submitNoButton() instead of submit().

            Here is the javadoc for your reference:
            http://httpunit.sourceforge.net/doc/api/com/meterware/httpunit/WebForm.html

            Stan
            http://www.jsfunit.org

            • 3. Re: Submitting form without submit button
              pieter.kuijpers

              There is a slight difference between submitNoButton of HttpUnit and our method: HttpUnit's submitNoButton is invoked on a Form, where our method is invoked on ClientFacade. We must have a way to identify the form that should be submitted, and my suggestion was to submit the form of the component in the parameter. So it would become

              clientFacade.submitNoButton(aComponentId)

              as opposed to HttpUnit's

              form.submitNoButton()


              If we allow an arbitray componentId as parameter to submitNoButton, it will be entirely valid to use a button as componentId, resulting in the confusing

              clientFacade.submitNoButton(buttonComponentId)


              Maybe it is best to only allow a form component id?

              clientFacade.submitNoButton(formId)

              looks pretty intuitive to me.

              Pieter

              • 4. Re: Submitting form without submit button
                ssilvert

                Yes, I like the idea of only allowing a formId. That will be quick and easy to add.

                Ajax components usually add parameters to the request before submitting. What would you think of having a method like this as well? Is that something your own javascript would commonly do?

                clientFacade.submitNoButton(formId, Map extraParams);

                Stan

                • 5. Re: Submitting form without submit button
                  pieter.kuijpers

                  I haven't come across this need yet, as I don't use Ajax a lot.

                  Wouldn't it be easier to be able to add extra parameters using clientFacade.setParameter or something like clientFacade.addExtraParameter(id, value)? That would make submitNoButton a little cleaner. Otherwise I can imagine a lot of submitNoButton(formId, null) calls in my code.

                  Pieter.

                  • 6. Re: Submitting form without submit button
                    ssilvert

                     

                    "pieter.kuijpers@gmail.com" wrote:
                    I haven't come across this need yet, as I don't use Ajax a lot.

                    Wouldn't it be easier to be able to add extra parameters using clientFacade.setParameter or something like clientFacade.addExtraParameter(id, value)? That would make submitNoButton a little cleaner. Otherwise I can imagine a lot of submitNoButton(formId, null) calls in my code.

                    Pieter.


                    I wouldn't allow submitNoButton(formId, null). If we did it, we would have two methods:
                    submitNoButton(formId);
                    submitNoButton(formId, Map extraParams)

                    For the second method, it would throw NullPointerException if extraParams is null.

                    For now, I think I'm going to just implement the first one. There is already a way to do the second one using the org.jboss.jsfunit.facade.WebRequestFactory. But it takes a little more work as you have to do:

                    ClientFacade client = new ClientFacade("/mypage.jsf");
                    
                    WebRequestFactory reqFactory = new WebRequestFactory(client);
                    PostMethodWebRequest request = reqFactory.buildRequest("myformid");
                    // manipulate the request (setParameter, removeParameter, setHeaderField, etc)
                    
                    client.doWebRequest(request);


                    Also, I'm thinking of removing the no-arg submit() method. We should encourage all forms to have an ID.

                    Stan Silvert
                    http://www.jsfunit.org

                    • 7. Re: Submitting form without submit button
                      ssilvert

                      I implemented ClientFacade.submitNoButton(). It didn't turn out the way we planned. In looking at the rest of the API, it was hard to justify the requirement of using a formID. The only place a specific kind of component is required is the ClientFacade.submit(submitButtionId). In that one, you need to specify which button you want to press. Everywhere else, including in the WebRequestFactory, you can just pass any componentId and it will find the form for you and do the right thing.

                      Also, I started to think, what if you don't know the form id or you can't easily get it?

                      Anyhow, what is there might be wrong, but let's try it and see how things go. I've updated the online javadoc so everyone can see the very latest for the Core APIs. You can view that here:
                      http://labs.jboss.com/file-access/default/members/jsfunit/freezone/apidocs/index.html

                      There is no doubt that this will change some more. By definition, a facade is a simplification of an existing API and its design comes down to taste. I find this kind of thing really hard because I'm not sure my taste is that good. But time will tell if developers like it. And it's easy to change...

                      Stan
                      http://www.jsfunit.org