You’ve told us how much you want a feature to dynamically build forms fully adapted to your end users’ needs. Well, we heard you, and 4D v16 R6 now opens new opportunities with dynamic forms! Provided as a preview in 4D v16 R6, dynamic forms allow you to programmatically build your forms in an object or load them from a text file. But this only scratches the surface of what we’ve cooked up for you…
Dynamic forms – Database example
dynamic form Benefits
In dynamic forms, the entire content of the form – pages, form objects, and the properties for each object – are described in a textual format (JSON format). Thus, dynamic forms can be stored in an object or in a file. For a smooth migration, it’s possible to have both types of forms in a database, i.e. both regular forms (stored in a binary format in the .4DB file) and dynamic forms (stored as text anywhere on disk).
Dynamic forms can be used with compiled or stand-alone applications. The advantage of the new forms vs. regular binary forms is that you can modify them just by updating the JSON file … without having to recompile the application. This gives you great flexibilty in deployment.
Using dynamic forms has other great benefits, too. Have you already had a customer who requested changing the font for all form objects? With dynamic forms, it’s as simple as doing a “Search and Replace” in a text file!
Now let’s say you want to email a particular form to a co-worker, you only have to send a single text file instead of the whole database package. Dynamic forms also makes it simple to reuse a form in another application without the hassle of moving it to another database … just copy/paste a single file and it’s done!
commands supporting dynamic forms
Henceforth, all 4D commands which use a form name have been enhanced to also accept a form description as an object or a file path:.
- DIALOG
- FORM SET INPUT
- FORM SET OUTPUT
- OBJECT SET SUBFORM
- OBJECT GET SUBFORM
- FORM LOAD
- Print form
- Current form name
“HELLO WORLD” Example
form description from a text file
Let’s take a simple example with just text and a button:
The textual description of the form is as follows:
{ ... list of form parameters ... "pages": [ ... list of form pages ... null, ... page 0 ... { ... page 1 ... "objects": { ... list of form objects in page 1 ... "text": { "type": "text", "text": "Hello World !", "left": 20, "top": 20, "width": 200, "height": 20 }, "button": { "type": "button", "text": "OK", "left": 120, "top": 40, "width": 100, "height": 20 } }}] }
And here’s the code to load this form in a dialog:
DIALOG ("/RESOURCES/helloWorld.json")
The grammar is defined by a JSON schema. The documentation also gives the complete list of attributes that can be used in dynamic forms, with details about which attributes are supported by each form object. Don’t forget that you can use the JSON Validate command to verify that the JSON in your form description is in accordance with the JSON schema.
Build a form in an object
Now, let’s make this form fully dynamic by building it only with code!
// Build form description
$label:=New object("type";"text";"text";"Hello World!";"top";20;"left";20;"width";200;"height";20)
$button:=New object("type";"button";"text";"OK";"top";40;"left";120;"width";100;"height";20)
$page:=New object("objects";New object("label";$label;"button";$button))
$form:=New object("pages";New collection(Null;$page);"windowTitle";"My first dynamic form";"rightMargin";20;"bottomMargin";20)
// Load the form in a dialog
$w:=Open form window($form)
DIALOG($form)
As you can see, it gives you the flexibility to build your forms according to your needs. Once you have written your forms in a file or an object, you can use them same as you would use regular forms.
What about editing forms?
For the moment, there’s no mechanism available to edit your dynamic forms other than using a text editor. But this won’t last! A form editor dedicated specifically to dynamic forms is on its way. When? Stay tuned.