Skip to content

Instantly share code, notes, and snippets.

@markadrake
Last active January 30, 2022 05:30
Show Gist options
  • Save markadrake/c8f58a891d218aacc03637c7fc67ffb9 to your computer and use it in GitHub Desktop.
Save markadrake/c8f58a891d218aacc03637c7fc67ffb9 to your computer and use it in GitHub Desktop.
Umbraco 9 > Umbraco Forms > Custom Confirmation Field Type

Page 1:

  • Text string entry for name
  • Checkbox entry for consent

image

Page 2:

  • Proof of concept for confirmation

image

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Http;
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Forms.Core;
using Umbraco.Forms.Core.Enums;
using Umbraco.Forms.Core.Models;
using Umbraco.Forms.Core.Providers;
using Umbraco.Forms.Core.Services;
namespace NSVSAC.Umbraco.WebApplication.App_Plugins.HumbleForms
{
public class ConfirmationFieldType : FieldType
{
public ConfirmationFieldType()
{
this.Id = new Guid("08b8057f-06c9-4ca5-8a42-fd1fc2a06eff"); // Replace this!
this.Name = "Confirmation";
this.Description = "Confirm previous entries before proceeding.";
this.Icon = "icon-autofill";
this.DataType = FieldDataType.Bit;
this.SortOrder = 10;
this.SupportsRegex = true;
}
public override string GetDesignView() => "~/App_Plugins/HumbleForms/confirmationField.html";
public override string RenderView => "~/App_Plugins/HumbleForms/confirmationField.html";
// You can do custom validation in here which will occur when the form is submitted.
// Any strings returned will cause the submit to be invalid!
// Where as returning an empty ienumerable of strings will say that it's okay.
public override IEnumerable<string> ValidateField(Form form, Field field, IEnumerable<object> postedValues, HttpContext context, IPlaceholderParsingService placeholderParsingService)
{
var returnStrings = new List<string>();
if (!postedValues.Any(value => value.ToString().ToLower().Contains("custom")))
{
returnStrings.Add("You need to include 'custom' in the field!");
}
// Also validate it against the original default method.
returnStrings.AddRange(base.ValidateField(form, field, postedValues, context, placeholderParsingService));
return returnStrings;
}
}
public class Startup : IComposer
{
public void Compose(IUmbracoBuilder builder)
{
builder.WithCollectionBuilder<FieldCollectionBuilder>()
.Add<ConfirmationFieldType>();
}
}
}
@using Umbraco.Core;
@using Umbraco.Cms.Core.Composing;
@using Umbraco.Forms.Core.Services;
@inject IFormService _formService;
@model Umbraco.Forms.Web.Models.FieldViewModel
@{
var currentFormRequest = Context.Request.Form;
var formId = currentFormRequest["FormId"];
var form = _formService.Get(new Guid(formId));
}
<pre>
@foreach(var f in form.AllFields)
{
var label = f.Caption;
var value = currentFormRequest[f.Id.ToString()];
<text>
@(label) / @(value)
</text>
}
</pre>
<input type="checkbox"
name="@Model.Name"
id="@Model.Id"
value="true"
data-umb="@Model.Id"
@if (Model.Mandatory) { <text> data-val="true" data-val-requiredcb="@Model.RequiredErrorMessage" </text> }
@if (Model.ContainsValue(true) || Model.ContainsValue("true") || Model.ContainsValue("on")) { <text> checked="checked" </text>} />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment