Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • jonok 297 posts 658 karma points
    Nov 01, 2022 @ 04:53
    jonok
    0

    Umbraco Forms V7 - skip empty pages

    I have a multi-page form, with each page showing a single question. There are conditions that cause some questions to be hidden, but this then leaves an empty page with just the the 'next' and 'back' buttons.

    Is it possible to extend the UmbracoFormsController so that the form skips past any empty pages? eg. by overriding the OnFormHandled method?

    Or is there another way that I should be trying to do this?

  • jonok 297 posts 658 karma points
    Nov 07, 2022 @ 04:46
    jonok
    100

    If anybody needs this in the future, this is the code I used:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net.Mail;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    using System.IO;
    using Umbraco.Forms.Core;
    using Umbraco.Forms.Mvc.Models;
    using Umbraco.Forms.Core.Models;
    using Umbraco.Forms.Web.Models;
    using Umbraco.Core;
    using Umbraco.Forms.Core.Providers;
    using Umbraco.Forms.Core.Services;
    using Umbraco.Core.Configuration.UmbracoSettings;
    using Umbraco.Core.IO;  
    using Umbraco.Forms.Core.Attributes;
    using Umbraco.Forms.Core.Controllers;
    using Umbraco.Forms.Core.Enums;
    
    namespace UmbForms.Extensions
    {
        public class UmbracoFormsExtController : Umbraco.Forms.Web.Controllers.UmbracoFormsController
        {
    
            protected override void OnFormHandled(Form form, FormViewModel model)
            {
                SkipEmptyPages(form, model);
            }
    
            private void SkipEmptyPages(Form form, FormViewModel model) {
                var fieldValues = model.FormState.ToDictionary(kvp => new Guid(kvp.Key), kvp => String.Join(",", kvp.Value));
                while (
                        model.CurrentPage != null &&
                        GetCurrentFields(model).Any() &&
                        (
                            NoFieldsetsShown(form, model, fieldValues) ||
                            NoFieldsShown(form, model, fieldValues)
                        )
                    )
                {
                    if (
                        (
                            !string.IsNullOrEmpty(Request["__prev"]) ||
                            !string.IsNullOrEmpty(Request["PreviousClicked"])
                        ) && 
                        model.FormStep > 0
                    )
                    {
                        model.FormStep--;
                        if (model.FormStep < 0)
                            model.FormStep = 0;
                    }
                    else
                    {
                        if (ModelState.IsValid)
                            GoForward(form, model, model.FormState);
                    }
                }
            }
    
            private static bool NoFieldsShown(Form form, FormViewModel model, Dictionary<Guid, string> fieldValues)
            {
                return CurrentFieldConditions(model).All(c => c != null && c.Enabled && !c.IsVisible(form, fieldValues));
            }
    
            private bool NoFieldsetsShown(Form form, FormViewModel model, Dictionary<Guid, string> fieldValues)
            {
                return CurrentFieldsetConditions(model).All(c => c != null && c.Enabled && !c.IsVisible(form, fieldValues));
            }
    
            private IEnumerable<FieldCondition> CurrentFieldsetConditions(FormViewModel model)
            {
                return model.CurrentPage.Fieldsets.Select(fs => fs.Condition);
            }
    
            private static IEnumerable<FieldCondition> CurrentFieldConditions(FormViewModel model)
            {
                return GetCurrentFields(model).Select(f => f.Condition);
            }
    
            private static IEnumerable<FieldViewModel> GetCurrentFields(FormViewModel model)
            {
                return model.CurrentPage
                    .Fieldsets
                    .SelectMany(fs => fs.Containers)
                    .SelectMany(c => c.Fields);
            }
    
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft