Is it possible to populate Episerver.Forms field with data

2.2k Views Asked by At

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

3

There are 3 best solutions below

0
Kristoffer Elde On BEST ANSWER

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" />
6
Eric Herlitz On

It is, but not out of the box. We typically solve this by creating custom elements.

David Knipe wrote an excellent blog on this, https://www.david-tec.com/2016/01/building-out-a-custom-form-element-with-the-new-episerver-forms/

Check the form model

public override void SetDefaultValues(ContentType contentType)
{
    base.SetDefaultValues(contentType);

    // override
    base.PredefinedValue = "Hello world";
}
0
Thach Lockevn On

The new Episerver Forms has Autofill API to help you do exactly that job.