Advanced Cocoon Forms: dynamic templates and recursive forms
Sylvain: cocoon commiter, apache member, cocoon pmc chair
Many people are using cocoon forms. Dynamic forms: how you can have your form template dependent on the contents.
Classical cocoon form pipeline: - Form generator - Form transformer
Doesn't require special generator but your form template cannot be dependent on what is in the form. Workaround is a transform after template generation. This is only possible for top level widgets.
Dynamic form templates implements cform template language in JX macros For example: when a repeater is empty, I don't want to show it in the table. New JX variables with ft:repeater-widget for repeater, widget(current row), and repeaterLoop(the JX:for-each iterator)
repeaterLoop.index => row number repeaterLoop.first => at first row? repeaterLoop.last => at last row?
Usable for instance for alternating css classes in table data, buttons for move up, move down. Appropiate buttons depending on row number.
Use Case: datasource chooser Form contents depend on another widget. F.i. filesource, jdbcsource. File needs filename, JDBC needs url and password. He uses discrimination widget combined with the new union widget, which references discrimination widgets as a CASE statement. Chooses widget depending on which value is selected in discrimination widget. Same thing in the form template. Also uses a submit on change, if the union case changes In the binding there is also a union. Bind the discriminant value first, and choose the datasource sub-binding depending on that value.
Use case: reusable form snippets. Example: task tree for project management. Task can have sub-tasks. Two new widgets: fd:class and fd:new. Class defines definition snippet, new instantiates the class. Useful anywhere where you have a tree of data, e.g. CMS, IDE,...
Works very well with union and repeater.
How is it done: * define different classes: first single task class * inside class definition is a fd:new that references itself * new just inlines class definition ( bug in slide, row-element should be inside widgets ) * define a class for tasklist, the toplevel container
Class and new are also available in form template and binding
Can other form frameworks do that
Don't think so. Try to do that with struts. Cocoon forms rulez! And there's more to come!
Pff he was moving fast. Hope I got the gist of it.
Q: Does the forms framework include form validation? A: Of course!
Last year's intro to Cocoon forms is still available on the website
Q: What about the client side? e.g. page reloads needed every time you have an onSubmit widget A: The round-trip approach has much more flexibility (and is easier) but there's no reason a client-side solution couldn't be developed.
Q: What if you generate your forms from external data such as database. Totally dynamic form definition. A: You can use a cocoon pipeline that extracts data from your database and generates a form definition.
Q: how much JS is on the client side? A: basically nothing (at the moment)
Q: Can you set the discriminating widget from flow? A: Yes. It's just a regular widget really.