9 Replies Latest reply on Jun 20, 2008 3:16 PM by Samuel Mendenhall

    Generate a Dynamic Form : advice needed

    Laurent 4x Newbie

      Hi !
      I'm working with seam and facelets trying to build a new application.
      My main trouble is that in our previous app (built with a proprietary (and really nasty) techno) I was able to do dynamic forms.

      By dynamic I mean that the form at the begining is really quite just a blank file.
      the items (input text; radio btns or checkboxes; drop-down etc..) are added in the page dynamically during the rendering based on some configuration computed from the database .
      Then with the help of some convention regarding the id s of my  widget I was able to have a method dealing with all that

      Nowadays , at last , after years of me beggin' the boss to face Java , Jee etc... we are out of this ancient proprietary and are in the world of  jboss, jsf and seam.  !

      Nice ; but how can I achieve the same kind of feature wich is mandatory in my case ?

      I was thinking about this :
      Imagine I got my

      * list of fields in some array  with them types and parameters

      * In my code i know that for this type of field i want to show an input field for example.

      * I put fragments/templates for each kind of widget  (input text; radio btns or checkboxes; drop-down etc..)

      * then trying to compose my main form.xhtml by including some of these dynamically
      If my current Fieldtype[] is with 4 input and 2 dropdown
      I add them

      * then add my Submit and my Cancel

      * On seam side building my methods as before so that with the help of the input fields id I can figure out which fields is concerned

      But I'm too newbie to seam and facelets to be sure
      Is this scenario possible ?
      If not where should I change it (as I think the whole idea is here anyway  : you don't got 1000s way to do that )?

      Can some most talented seam user tell me where I should look for doing these two task :
      compose my form dynamically
      and best way (seam-side) to handle the non standard form on validation .

      So of course any help is welcomed.

      Thanks you all !
      PS : May be I should also found a facelets forum to ask  there ?

        • 1. Re: Generate a Dynamic Form : advice needed
          Daniel Roth Apprentice

          I would probably have built small facelet compositions per part which were included/excluded with the rendered-flag in the mother-composition.

          • 2. Re: Generate a Dynamic Form : advice needed
            Laurent 4x Newbie

            Hello Daniel;

            But how do you deal with the fact that there can be let say
            2 input fields then one group of check boxes then 1 other input
            and for a second entry could be just a text area ?
            I can't just put composition by design time

            indeed, the order AND the type of the dynamically-rendered fields are set on runtime

            may be on composition I can use EL to design the part; then
            I put let'say 50 composition tag on my mother-page then some style of

            or <ui:composition with template=#{formDefinition.get1stType}>
            #{formDefinition.get2ndType; #{formDefinition.get3rdType}
            and so on for let say may maximum of 50 fields per page.

            Not really nice but may be it can works if the EL is permitted in this case ?

            So the rendered-flag will strictly be used only as a property of a field depending on some extra meta data filled in formDefinition.get1stMetadata ..to.. formDefinition.get50thMetadata

            What do you think about it ?

            • 3. Re: Generate a Dynamic Form : advice needed
              Cloves Almeida Newbie

              My 2cents,

              1) You can use either JSF UI bindings or a mix of ui:include and ui:repeat.

              <ui:repeat value="#{formDefinition.types}" var="type">
                <ui:include template="#{type.template}" />

              2) Don't try to force the old paradigm when changing technologies. Think again and again if such feature is needed.

              • 4. Re: Generate a Dynamic Form : advice needed
                Laurent 4x Newbie

                for your 2nd point :
                Yes ! unfortunately I have to !
                It's one the application main feature
                The application claims to let user add custom fields when defining their main entities;

                So the real content of my entities can not be guessed by compile time. You have to go grab some data from the configuration table to know what the Admin user had set up.
                And then must create your form (input/output) according to this

                I'm going to go read some resource; then start to try ! What I'm trying to do here is ask and get some good advice about what should be best fit and most important what is known for sure to be useless and dead-end !!
                It can save some time not only for me I think this subject can be of interest to others also.

                So thanks for your first point ! It is the kind of thing I was thinking about.. good to know it seems reasonable.

                • 5. Re: Generate a Dynamic Form : advice needed
                  Cloves Almeida Newbie

                  I can see the business case of your request.

                  If ui:include does not suit you, using ui:fragment and the binding attribute might be a more powerful alternative.

                  <ui:fragment binding="#{component}" />

                  And you dynamically create the components in Java:

                  import javax.faces.component.*
                  import javax.faces.component.html.*
                  public UIComponent getComponent() {
                      if (type=NORMAL_TEXT) {
                          HtmlInputText txt = new HtmlInputText();
                          txt.setValue("hello jsf");
                          return txt;
                      } else if (type=CUSTOM_DATE){
                          MyCustomDate c = new MyCustomDate();
                          c.setValue(new Date());
                          return c;

                  • 6. Re: Generate a Dynamic Form : advice needed
                    Przemyslaw Jaskierski Newbie


                    1) Will not work. It would work with c:forEach, but not ui:repeat. Using c:forEach brings a lot of pain, though.

                    Dynamic templating sucks in facelets bigtime.

                    • 7. Re: Generate a Dynamic Form : advice needed
                      eli hamburger Newbie
                      Has anyone come up with ideas on how to get around the <ui:repeat> with <ui:include> problem?

                      • 8. Re: Generate a Dynamic Form : advice needed
                        Laurent 4x Newbie

                        I didn't get the time to work on it yet;
                        should be next week BUT I've seen this post :


                        He did what we are thinking about I asked on his thread to give us some hints about the ideas .

                        go on his thread; try it out and please : let us know if you succeed !

                        The more we share on this the best it will be

                        • 9. Re: Generate a Dynamic Form : advice needed
                          Samuel Mendenhall Apprentice

                          Dynamic templating sucks in facelets bigtime.

                          Dynamic templating sucks until you get it then the possibilities are endless, truly, more soon.