1

I need to create a form that takes some data from the server and generate a form based on the type of the data. For example, if I have a String, I need the form to have an input box. If I have a List, I need the corresponding form field to have a group of checkboxes. Is there anyway to create this type of auto-generating form? I have been playing with data tables, and I can figure out how to generate a table that dynamically changes. But I can't find a way to generate a form that dynamically changes. I can probably figure out a way to create this in javascript/jquery, but is there a way using JSF/PrimeFaces/RichFaces?

user2471366
  • 73
  • 12

2 Answers2

2

This feature might interest you, it's a primefaces extension that handles creation of dynamic forms (on runtime). The form receives fields (or controls) by wish from a bean and adds them.

It's called DynaForm ; hope that helps your cause.

Akheloes
  • 1,352
  • 3
  • 11
  • 28
  • Thanks! This is very similar to what I was looking for. (Though you guys did bring up a good point about handling this with Javascript instead). – user2471366 Jun 21 '13 at 15:04
  • jQuery will be ideal to this sort of things, along with ajax and JSON (to hit the DB from the dynamic forms). – Akheloes Jun 21 '13 at 23:52
0

You can take advantage of the rendered attribute, doing something like this:

<h:inputText rendered="#{bean.doIHaveAString}" />

<h:selectManyCheckbox rendered="#{bean.doIHaveAList}" />

those elements will only appear if the condition is met

Makhiel
  • 3,874
  • 1
  • 15
  • 21
  • I guess my problem is that there are no fixed inputs. So someone might request a form with 4 Strings, and someone else might request a form with 4 Lists. So for the form with 4 Strings, I would need 4 inputText arguments, and for the form with 4 Lists, I would need 4 selectManyCheckboxes. Is there a way to do that? – user2471366 Jun 20 '13 at 15:39
  • @user2471366 this kind of pages are better handled using JavaScript, not a server side technology (like JSF). – Luiggi Mendoza Jun 20 '13 at 16:27
  • Indeed, JavaScript or JSP seem to be better disposed for this kind of use cases. – Akheloes Jun 21 '13 at 05:50
  • Yes, good point. I was just wondering whether JSF could handle it. Thanks. – user2471366 Jun 21 '13 at 15:03