Skip to content

Instantly share code, notes, and snippets.

@lars-erik
Created June 2, 2016 08:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lars-erik/7a0028f28b93385574b68ce11276bd7a to your computer and use it in GitHub Desktop.
Save lars-erik/7a0028f28b93385574b68ce11276bd7a to your computer and use it in GitHub Desktop.
Conditional pages for Umbraco Forms
using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Forms.Core;
using Umbraco.Forms.Mvc.Models;
using Umbraco.Forms.Web.Controllers;
namespace UmbForms.Extensions
{
public class UmbracoFormsExtController : UmbracoFormsController
{
protected override void OnFormHandled(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 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 bool NoFieldsShown(Form form, FormViewModel model, Dictionary<Guid, string> fieldValues)
{
return CurrentFieldConditions(model).All(c => c != null && c.Enabled && !c.IsVisible(form, fieldValues));
}
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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment