Virtual Happy Hour this month, Jun 28, we'll be getting a sneak preview at our soon to launch SaaS CMS!

Try our conversational search powered by Generative AI!

Possible for editor to "extend" a form template with additional elements?

Vote:
 

Our site has around 80 forms. These forms have a lot of "standard" form elements that are the same across each form (name, phone number, email etc.).

Is it possible to have an editor-editable "base form" as a settings object, that the editor  can then extend with additional form elements on pages that require it? And importan caveat is that if an editor changes the base form, this change should be reflected in all other forms, that utilize the base form as its template. Is this possible? Thanks!

Base form in settings-block: 

  • Form Element A
  • Form Element B

Form 1:

  • Form Element A
  • Form Element B
  • ---------------------
  • Form Element C

Form 2:

  • Form Element A
  • Form Element B
  • ---------------------
  • Form Element D

Editor changes the base form to:

Base form in settings-block: 

  • Form Element X
  • Form Element B

So Form 1 now looks like:

Form 1:

  • Form Element X
  • Form Element B
  • ---------------------
  • Form Element C
#267783
Edited, Dec 03, 2021 13:52
Vote:
 

This is a great idea and also possible if you create your a custom Wrapping Forms Container Block.

Have a look at this commit whch adds the files for a custom Forms container: Custom DataCaptureForm Container block with razor template · johnnymullaney/Foundation@2fdb719 (github.com)

  1. In the Forms Container Block definition add a ContentReference "Base Form" property which Allows Type Form
  2. In the Controller you could implement the logic to merge both IForm.ISteps 
#267786
Edited, Dec 03, 2021 15:29
jly@immeo.dk - Dec 06, 2021 11:31
Hi Johnny, thanks for the input! Unfortunately, your second link leads to a 404, and I'm a bit lost as to how I merge the two forms. I'm attempting to concat the Elements of the last step of the base form, with the Elements of the form I'm extending with, and with the debugger attached I see that the elements are added, but they fail to appear on the page.
Vote:
 

What I've attempted so far is the create a new form, update its ElementsArea with the elements from the new form, and updated its steps with the elements from the new form as well. The added fields render on the page, but on submission, the submission data does not contain the new items. Any advice?

        public override ActionResult Index(ILeadCallToActionButtonBlock currentBlock)
        {
            
            var baseForm = _contentLoader.Get<ExtendedFormContainerBlock>(currentBlock.LeadBaseForm).CreateWritableClone() as ExtendedFormContainerBlock;
            var addOnForm = _contentLoader.Get<ExtendedFormContainerBlock>(currentBlock.LeadFormSpecificElements?.Items[0]?.ContentLink);
            foreach (var addOnItem in addOnForm.ElementsArea.Items)
            {
                baseForm.ElementsArea.Items.Add(new ContentAreaItem()
                    { ContentLink = addOnItem.ContentLink });
            }


            var baseFormLastStep = baseForm.Form.Steps.Last();
            var baseFormLastStepElements = baseFormLastStep.Elements;
            var addOnFormElements = addOnForm.Form.Steps.First().Elements.ToList();
            foreach (var addOnFormElement in addOnFormElements)
            {
                addOnFormElement.Form = baseForm.Form;
            }

            var combinedElements = baseFormLastStepElements?.Concat(addOnFormElements);

            var newForm = baseForm.Form.Steps;
            newForm.Last().Elements = combinedElements;

            baseFormLastStep.Elements = newForm;

            var viewModel = new LeadCallToActionButtonViewModel
            {
                LeadForm = baseForm,
                ButtonColor = currentBlock.Color,
                ButtonText = currentBlock.ButtonText,
                TopText = currentBlock.TopText,
                Image = currentBlock.Image,
                Anchor = currentBlock.Anchor,
                Id = new Guid()
            };
            base.Index(currentBlock);
            return PartialView(viewModel);
        }
#267975
Dec 07, 2021 14:11
Johnny Mullaney - Dec 10, 2021 12:33
Apologies, missed this message. if you want to continue on this path you should override the DataSubmissionService and debug through to see if you can identify how the merged form elements are handled. I haven't coded this requirement so am not totally sure why the new fields are not mapping. Looking to see whats going on in the service will hopefully give you a clue on where to go next.

Although I would suggest you consider abandoning the approach of inheriting Forms and double checking with your customer how important it is to do it this way. If you keep customising you will create technical debt that may not be upgrade friendly in the future.

It's not exactly what you are looking for but you can create a "Base Form" and instruct CMS Editors to simply create new Forms by copying that form.

This simple approach doesnt give you the ability to update the Base Form and have it published to all Forms. Check in with your customer to see if this is just a minor inconvenience. If you need to keep going, use content events to detect updates to the base form and update all other Forms. It might be a bit messy this way but is possible

https://world.optimizely.com/blogs/Daniel-Ovaska/Dates/2019/6/content-events-in-episerver/
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.