Skip to content

Instantly share code, notes, and snippets.

@bogdanstojanovic
Last active December 29, 2018 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bogdanstojanovic/c48ada3bc38b08d2c837b9b1b842d165 to your computer and use it in GitHub Desktop.
Save bogdanstojanovic/c48ada3bc38b08d2c837b9b1b842d165 to your computer and use it in GitHub Desktop.
Consent Form
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web;
using EPiServer.Core;
using EPiServer.DataAnnotations;
using EPiServer.Forms;
using EPiServer.Forms.Core.Internal;
using EPiServer.Forms.Core.Models.Internal;
using EPiServer.Forms.EditView;
using EPiServer.Forms.EditView.DataAnnotations;
using EPiServer.Forms.Helpers.Internal;
using EPiServer.Forms.Implementation.Elements.BaseClasses;
using EPiServer.Forms.Implementation.Validation;
namespace EpiCustomFormFieldMapping.Models.Blocks
{
[ContentType(DisplayName = "Consent Form Element", GUID = "be596250-fda6-43ae-af5b-ff17cb647f7a", GroupName = "Custom form elements", Order = 1000)]
[AvailableValidatorTypes(Include = new[] { typeof(RequiredValidator) })]
public class ConsentElementBlock : ValidatableElementBlockBase, IElementRequireClientResources, IUIExternalFieldMapping
{
[CultureSpecific]
[Display(Order = 100)]
public virtual XhtmlString ConsentText { get; set; }
[CultureSpecific]
[Display(Order = 110)]
public virtual string ConsentRequired { get; set; }
[ScaffoldColumn(false)] public override string Label { get; set; }
[ScaffoldColumn(false)] public override string Description { get; set; }
public override string Validators
{
get
{
var privacyValidator = typeof(ConsentValidator).FullName;
var validators = this.GetPropertyValue(content => content.Validators);
if (string.IsNullOrEmpty(validators))
{
return privacyValidator;
}
return string.Concat(validators, Constants.RecordSeparator, privacyValidator);
}
set
{
this.SetPropertyValue(content => content.Validators, value);
}
}
public IEnumerable<Tuple<string, string>> GetExtraResources()
{
return new List<Tuple<string, string>>()
{
new Tuple<string, string>("script", "/Static/js/consent-form-field-validator.js")
};
}
public override object GetSubmittedValue()
{
var rawSubmittedData = HttpContext.Current.Request.Form;
var isJavaScriptSupport = rawSubmittedData.Get(Constants.FormWithJavaScriptSupport);
if (isJavaScriptSupport == "true")
{
return base.GetSubmittedValue();
}
var privacyConsentComponents = rawSubmittedData.GetValues(Content.GetElementName());
if (privacyConsentComponents == null || privacyConsentComponents.Length < 1)
{
return null;
}
return privacyConsentComponents[0];
}
public virtual object GetFormattedValue()
{
var submittedValue = GetSubmittedValue() as string ?? string.Empty;
return submittedValue;
}
public override ElementInfo GetElementInfo()
{
var baseInfo = base.GetElementInfo();
baseInfo.CustomBinding = true;
return baseInfo;
}
}
}
@model EpiCustomFormFieldMapping.Models.Blocks.ConsentElementBlock
@using System.Web.Mvc
@using EPiServer.Forms.EditView.Internal
@using EPiServer.Forms.Helpers.Internal
@using EPiServer.Forms.Implementation.Elements
@using EPiServer.ServiceLocation
@{
var formElement = Model.FormElement;
var validationService = ServiceLocator.Current.GetInstance<ValidationService>();
}
<div class="Form__Element Form__CustomElement Form_Privacy FormChoice form-group @validationService.GetValidationCssClasses(Model)" id="@Model.FormElement.Guid" data-epiforms-element-name="@formElement.ElementName">
<div class="m-checkbox-container">
<input id="@String.Format("{0}_{1}",formElement.Guid,"privacyelement")" type="checkbox" name="@formElement.ElementName" value="ConsentCollected" class="FormChoice__Input FormChoice__Input--Checkbox" />
<label for="@String.Format("{0}_{1}",formElement.Guid,"privacyelement")" style="display: inline;">@Html.PropertyFor(x => x.ConsentText)</label>
</div>
</div>
<span data-epiforms-linked-name="@formElement.ElementName" class="Form__Element__ValidationError" style="display: none;">*</span>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment