Prefill Dynamic Forms with through code?

Topics: General
Dec 26, 2015 at 9:53 AM
Is it possible to prefill Dynamic Forms through code?
For example, I have User Account Information which is not stored in Orchard, but in another application.

I want to provide an 'Update my account' dynamic form, which should be prefilled with all information (Orchard-internal and Orchard-external) of the account.

Currently I'm thinking about the following solution:
  • providing a Web API path to retrieve User Information
  • add javascript to the dynamic form to retrieve the User Information and fill the form in JavaScript
Is this a good way to solve this? Is there a more efficient way? Can I make sure the fields are already prefilled before the form is loaded?

Many thanks in advance!

Nathan
Developer
Dec 26, 2015 at 12:50 PM
That solution would work just fine I suppose. Another solution could be to implement an ElementDriver<Form> and implement the OnDisplaying method. This method receives the Form element about to be displayed, and would give you a chance to pre-populate the various fields (by traversing the element tree, for which you'd have to write a function so you can find form elements by name. Since all form elements derive from FormElement, they all have a Name property, so that should be trivial enough).

However, the problem with that approach is the hardcoded knowledge of the available fields on your form. Far better would be to give each form element knowledge of where to get their value from. In this solution, you would implement your own ElementHarvester that harvests form field elements based on the available field from your external system. You'd essentially iterate over each "external field" definition, and configure the element's descriptor such that their Displaying handler know how to fetch the current user's information for that particular field.

With that in place, you can now design forms by dragging & dropping application-specific form fields (like Name, Phonenumber, City, etc.), and these fields would automatically show the currently authenticated user's details.

Handling the update of the user account's details in the external application could be done as follows:
  • Implement FormEventHandlerBase and override the Validated method.
  • In that method, get all FormElement instances using IFormService.GetFormElements().
  • Depending on your API into the external system, you now have all the submitted data available to you. Your system probably doesn't support updating individual fields per call, but likely takes some DTO containing all of the updated user information. You can construct that call and such DTO from this Validated method and invoke the update.
Developer
Dec 26, 2015 at 12:52 PM
(by traversing the element tree, for which you'd have to write a function so you can find form elements by name. Since all form elements derive from FormElement, they all have a Name property, so that should be trivial enough).
Actually, you could just use the IFormService.GetFormElements method of course to get back a flat list of elements, which you can even project into a dictionary, since each IFormElement has a Name property.
Dec 27, 2015 at 9:59 AM
Thanks for the answer, this really provided some insight in to the problem I'm trying to solve.
For our first deadline, I'll go with the first solution you provided, but I'll definitely switch it to the more proper solution later on.

Thanks again, this is really good stuff!