Calling all developers! We invite you to provide your input on Feature Experimentation by completing this brief survey.

 

K Khan
Jan 19, 2016
  6459
(1 votes)

Color Palette for Forms

There are few blogs have already been written to extend new EPiServer forms. This blog is to extend the concept of customization in Form and providing an element to let the users select a color from color pallete with four easy steps.

Steps:

1. Extend a text element

using System.ComponentModel.DataAnnotations;
using EPiServer.DataAbstraction;
using EPiServer.DataAnnotations;
using EPiServer.Forms.Implementation.Elements;
using EPiServer.Shell.ObjectEditing;

namespace AlloyDemoKit.Forms
{
    [ContentType(
        DisplayName = "Color Picker Textbox",
        GroupName = "Form Elements",
        GUID = "70740CE9-7FEE-4582-B224-BAFC0D926889")]
    public class ColorPickerElementBlock : TextboxElementBlock
    {
    } 
}

2. Add language elements

<languages>
  <language name="English" id="en">    
    <contenttypes>
      <colorpickerelementblock>
        <name>Color Picker</name>
        <description />
      </colorpickerelementblock>
    </contenttypes>
  </language>
</languages>

3. Create a view (Views/Shared/Blocks/ColorPickerElementBlock.cshtml

@model AlloyDemoKit.Forms.ColorPickerElementBlock
@using EPiServer
@using AlloyDemoKit.Models.Pages.Models.Blocks
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dijit/themes/claro/claro.css" media="screen">
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js" data-dojo-config="isDebug: 1, async: 1, parseOnLoad: 1"></script>
<script>
    require(["dijit/ColorPalette", "dojo/dom", "dojo/on", "dojo/query", "dojo/NodeList-dom", "dojo/domReady!"], function (ColorPalette, dom, on, query) {
        var node = dom.byId("@Model.FormElement.Guid");
        var myPalette = new ColorPalette({
            palette: "7x10",
            onChange: function (val) {
                node.value = val;
                query("#placeHolderForColorPalette").style("display", "none");
            }
        }, "placeHolderForColorPalette");
        query("#placeHolderForColorPalette").style("display", "none");
        on(node, "focus", function () {            
                query("#placeHolderForColorPalette").style("display", "");
                myPalette.startup();
        });
   });
</script>
<div class="Form__Element FormTextbox" data-epiforms-element-name="@Model.FormElement.Code">
    <label for="@Model.FormElement.Guid">
        @Html.Translate("/colorpickerelementblockelements/selectcolor")
    </label>    
    <input type="text" name="@Model.FormElement.Code" class="FormTextbox__Input" id="@Model.FormElement.Guid" @Model.FormElement.AttributesString />
    <span data-epiforms-linked-name="@Model.FormElement.Code" class="Form__Element__ValidationError" style="display: none;">*</span>
    <div><span id="placeHolderForColorPalette"></span></div>
</div>

4. Other language elements

<colorpickerelementblockelements>
<selectcolor>My Favourite Color</selectcolor>
</colorpickerelementblockelements>

Ready to go

Image favouritecolor.PNG

Image favouritecoloredit.PNG

Scholarly articles:
Building out a custom form element with the new Episerver forms
Creating custom form elements in Episerver.Forms
Custom EPiServer Forms field

Jan 19, 2016

Comments

May 22, 2017 10:44 PM

Please login to comment.
Latest blogs
Level Up with Optimizely's Newly Relaunched Certifications!

We're thrilled to announce the relaunch of our Optimizely Certifications—designed to help partners, customers, and developers redefine what it mean...

Satata Satez | Jan 14, 2025

Introducing AI Assistance for DBLocalizationProvider

The LocalizationProvider for Optimizely has long been a powerful tool for enhancing the localization capabilities of Optimizely CMS. Designed to ma...

Luc Gosso (MVP) | Jan 14, 2025 | Syndicated blog

Order tabs with drag and drop - Blazor

I have started to play around a little with Blazor and the best way to learn is to reimplement some old stuff for CMS12. So I took a look at my old...

Per Nergård | Jan 14, 2025

Product Recommendations - Common Pitfalls

With the added freedom and flexibility that the release of the self-service widgets feature for Product Recommendations provides you as...

Dylan Walker | Jan 14, 2025