Dac Thach Nguyen
Dec 25, 2018
  5274
(6 votes)

EPiServer Forms: How to render Forms in a dialog

In some real scenarios, we might need render an EPiServer Forms within a dialog instead of a page. This case will make user focus on the Form. Today I have tried to make the Form work with jQuery UI dialog. Here a some point I summarized to make it works (assume that you are working with Alloy solution).

Create a page type to render an ContentArea with a Form

[SiteContentType(GroupName = Global.GroupNames.News, GUID = "E5CCD734-81F2-4242-82BA-8D888504112B")]
public class SubcriblePage: SitePageData
{
    [Display(GroupName = SystemTabNames.Content, Order = 320)]
    public virtual ContentArea MainContentArea { get; set; }
}

Then create a page with the page type then dnd a Form into it, publish it (let say it url is: "/en/subcrible/".)

Create a zaror layout to render a page which display nothing but Forms (_Empty.cshtml):

@using System.Web.Optimization
@using EPiServer.Framework.Web.Mvc.Html
@model IPageViewModel<SitePageData>
<!DOCTYPE html>
<html lang="@Model.CurrentPage.LanguageBranch">
<head>
    @Styles.Render("~/bundles/css")
    @Scripts.Render("~/bundles/js")
    @Html.RequiredClientResources("Header")
</head>

<body>
    @RenderBody()
    @Html.RequiredClientResources("Footer")
</body>
</html>

Create a view to render for the page type (SubcriblePage)

@{ Layout = "~/Views/Shared/Layouts/_Empty.cshtml"; }
@using ILMTest
@model PageViewModel<SubcriblePage>

@Html.PropertyFor(x => x.CurrentPage.MainContentArea, new { })
<script>
    if (typeof $$epiforms !== 'undefined') {
        $$epiforms(document).ready(function myfunction() {
            $$epiforms(".EPiServerForms").on("formsSubmitted", function (event, param1, param2) {
                window.parent.$(window.parent.document.body).trigger("submitted");
            });
        });
    }
</script>

This razor view will render an ContentArea with the Form. Add the script will notify parent window to know when the form already submitted (we will close the dialog then).

Open the dialog with a Form (Using iframe which loaded the page alread created above)

<script type="text/javascript">
    var page = "/en/subcrible/";

    var $dialog = $('<div></div>')
        .html('<iframe style="border: 0px; overflow: hidden;" src="' + page + '" width="100%" height="100%"></iframe>')
        .dialog({
            autoOpen: false,
            modal: true,
            height: 450,
            width: 500,
            title: "Some title"
        });
    $dialog.dialog('open');

    $(document).on("submitted", "body", function () {
        setTimeout(function () {
            $dialog.dialog("close");
        }, 1000)
    });
</script>

And here is result:

Dec 25, 2018

Comments

Kane Made It
Kane Made It Jan 4, 2019 01:57 AM

As awesome as always captain Rocky :D

Luc Gosso (MVP)
Luc Gosso (MVP) Jan 23, 2019 03:03 PM

Hey Rocky, Good work, just a friendly comment:

$$epiforms(".EPiServerForms").on("formsSubmitted" is not enough

You have to check the event like this:

 $$epiforms(document).ready(function closeParentModal() {
            $$epiforms(".EPiServerForms").on("formsSubmitted", function (event) {
                if (event.isFinalizedSubmission && event.isSuccess)
                window.parent.$(window.parent.document.body).trigger("submitted");
            });
        });

Or else it will close when captcha is used and wrong or if use of "steps"

Br Luc Gosso

Luc Gosso (MVP)
Luc Gosso (MVP) Jan 23, 2019 03:08 PM

Another thing, the "subscribe" page needs to be a Episerver PageData as in your example else @Html.RequiredClientResources("Header") wont work.

Is there a way to get @Html.RequiredClientResources("Header") to work on an standard MVC view or a Razor "Webpage" 3 view?

@Html.RequiredClientResources("Header") seems to be needing a episerver context page, why is that? (Question to Forms team)

Please login to comment.
Latest blogs
Optimizely Forms: You cannot submit this form because an administrator has turned off data storage.

Do not let this error message scare you, the solution is quite simple!

Tomas Hensrud Gulla | Oct 4, 2024 | Syndicated blog

Add your own tools to the Optimizely CMS 12 admin menu

The menus in Optimizely CMS can be extended using a MenuProvider, and using the path parameter you decide what menu you want to add additional menu...

Tomas Hensrud Gulla | Oct 3, 2024 | Syndicated blog

Integrating Optimizely DAM with Your Website

This article is the second in a series about integrating Optimizely DAM with websites. It discusses how to install the necessary package and code t...

Andrew Markham | Sep 28, 2024 | Syndicated blog

Opticon 2024 - highlights

I went to Opticon in Stockholm and here are my brief highlights based on the demos, presentations and roadmaps  Optimizely CMS SaaS will start to...

Daniel Ovaska | Sep 27, 2024

Required fields support in Optimizely Graph

It's been possible to have "required" properties (value must be entered) in the CMS for a long time. The required metadata haven't been reflected i...

Jonas Bergqvist | Sep 25, 2024

How to write a bespoke notification management system

Websites can be the perfect vehicle for notifying customers of important information quickly, whether it’s the latest offer, an operational message...

Nicole Drath | Sep 25, 2024