magnolia

Reusable Form Component in Magnolia


I use the form component to create a form and insert the data in a database with a JavaScript form processor.

I would like to create a reusable form with a fixed set of fields as a component that I can add to any page without filling the form dialog and without having to add the fields manually in each form component.

I can create an ftl with the required form fields but how can I store the data of the dialog in the component to avoid having to fill it everytime the form is inserted?


Solution

  • I see two ways to do this:

    a) Custom data handling through a model class You can create a custom component containing a form with your input elements. This will have nothing to do with the form module though. In order to process the data, the easiest way is to configure a modelClass for your component an in it implement the "executeEarly()" method in the model class. In there, you can do whatever you need with the data transmitted by the form (You'll find the data in the MgnlContext).

    b) Auto-generate from sub-components If you prefer to use the form module for the data handling, you could create your custom form component which extends the regular form component and then auto-creates the sub-components (i.E. input element components) you need.