Search code examples
episerverepiserver-forms

Is it possible to populate Episerver.Forms field with data


I'm using Episerver 10 together with Episerver.Forms addon. I created a form which contains several fields. I wonder if there's an option to prepopulate those fields with data.

Example: A field for email address. If user is logged in I want this field to has email from user profile set as a value.

I created my own custom field for email and I tried to use SetDefaultValues method but apparently it's meant for something different.

EDIT:

My Custom form field:

public class PostalCodeFormField : TextboxElementBlock
{
    public override void SetDefaultValues(ContentType contentType)
    {
        base.SetDefaultValues(contentType);
        base.PredefinedValue = "Hello world";
    }
}

Template for that field:

@model My.Valid.Namespace.PostalCodeFormField
<div class="Form__Element FormTextbox" data-epiforms-element-name="@Model.FormElement.ElementName">
    <label for="@Model.FormElement.Guid">
        My label
    </label>
    <p>@Model.PredefinedValue</p>
    <input type="text" name="@Model.FormElement.ElementName" class="FormTextbox__Input" id="@Model.FormElement.Guid" value="@Model.PredefinedValue" />
    <span data-epiforms-linked-name="@Model.FormElement.ElementName" class="Form__Element__ValidationError" style="display: none;">*</span>
</div>

Thing is @Model.PredefinedValue is empty like SetDefaultValues was never called


Solution

  • If you don't want to create a custom element (but i think that is the best solution) you can change the view of the element. It should be located under: \modules \ _protected \ EPiServer.Forms \ Views \ ElementBlocks

    For your email example i would change the TextboxElementBlock.ascx into something like this:

    <%@ import namespace="System.Web.Mvc" %>
    <%@ import namespace="EPiServer.Web.Mvc.Html" %>
    <%@ import namespace="EPiServer.Forms.Core.Models" %>
    <%@ import namespace="EPiServer.Forms.Helpers" %>
    <%@ import namespace="EPiServer.Forms.Implementation.Elements" %>
    <%@ Import Namespace="EPiServer.Personalization" %>
    <%@ control language="C#" inherits="ViewUserControl<TextboxElementBlock>" %>
    
    <%
        var formElement = Model.FormElement;
        var labelText = Model.Label;
        var isEmailField = formElement.Validators.Any(x => x is EPiServer.Forms.Implementation.Validation.EmailValidator);
        var defaultValue = Model.GetDefaultValue();
        if (isEmailField)
        {
            defaultValue = EPiServerProfile.Current.Email;
        }
    %>
    
    <div class="Form__Element FormTextbox <%: Model.GetValidationCssClasses() %>" data-epiforms-element-name="<%: formElement.ElementName %>">
        <label for="<%: formElement.Guid %>" class="Form__Element__Caption"><%: labelText %></label>
        <input name="<%: formElement.ElementName %>" id="<%: formElement.Guid %>" type="text" class="FormTextbox__Input"
            placeholder="<%: Model.PlaceHolder %>" value="<%: defaultValue %>" <%: Html.Raw(Model.AttributesString) %> />
    
        <span data-epiforms-linked-name="<%: formElement.ElementName %>" class="Form__Element__ValidationError" style="display: none;">*</span>
        <%= Model.RenderDataList() %>
    </div>
    

    Edit:

    Didn't see you edit the question. Remove the "base" from base.PredefinedValue in SetDefaultValues and it should work.

    public class PostalCodeFormField : TextboxElementBlock
    {
        public override void SetDefaultValues(ContentType contentType)
        {
            base.SetDefaultValues(contentType);
            PredefinedValue = "Hello world";
        }
    }
    

    For setting a "dynamic value you can add a new property to the class (getting current user email here):

      public virtual string UserEmail => EPiServerProfile.Current != null ? EPiServerProfile.Current.Email : string.Empty;
    

    And update the input in the view:

    <input type="text" name="@Model.FormElement.ElementName" class="FormTextbox__Input" id="@Model.FormElement.Guid" value="@Model.UserEmail" />