Forms and WebForms pt3

And the POC and learning continues. I did not like hiding the WebForms form, and it turns out that’s not necessary. You can include the Form controls in \Views\Shared\ElementBlocks.

In the FormContainerBlock.ascx I replaced the form tag with a div tag. This will be made configurable in a upcoming version of the Forms add-on, so you won’t have to include the controls in your project if you don’t want to when it is released.

Instead of hiding the WebForms form you can now set the attributes that were rendered on the Forms form on the WebForms form like this, which makes it a lot cleaner.

string validationFailCssClass = string.Empty;

if ((this.FakeContext.Controller.ViewBag != null)
                && (this.FakeContext.Controller.ViewBag.ValidationFail != null))
{
       validationFailCssClass = this.FakeContext.Controller.ViewBag.ValidationFail ? "ValidationFail" : string.Empty;
}

this.Page.Form.ID = this.CurrentBlock.Content.ContentGuid.ToString();
this.Page.Form.ClientIDMode = ClientIDMode.Static;
this.Page.Form.Attributes.Add("data-epiforms-type", "form");
this.Page.Form.Attributes.Add("enctype", "multipart/form-data");
this.Page.Form.Attributes.Add("class", string.Format(CultureInfo.InvariantCulture, "EPiServerForms {0}", validationFailCssClass));

I also rewrote the creation of the controller and the context. Now I can call the Index action on it, which takes care of registering the necessary resources, as they are done in the controller.

IControllerFactory factory = DependencyResolver.Current.GetService<IControllerFactory>()
                                             ?? new DefaultControllerFactory();

RequestContext requestContext = HttpContext.Current.GetRequestContext();

RouteData routeData = requestContext.RouteData ?? new RouteData();

if (!routeData.Values.ContainsKey("controller"))
{
      routeData.Values.Add("controller", "FormContainerBlock");
}

if (!routeData.Values.ContainsKey("action"))
{
     routeData.Values.Add("action", "Index");
}

FormContainerBlockController controller =
                    factory.CreateController(requestContext, "FormContainerBlock") as FormContainerBlockController;

ControllerContext newContext = new ControllerContext(httpContextBase, routeData, controller);
if (controller != null)
{
      controller.ControllerContext = newContext;
      return controller;
}

I noticed one thing, a javascript error is thrown in the “test” function that is rendered. It disappeared when I did a test with form steps. So if you see that error in a single step form, adding a step may help you out.
Instead of more gists I have created a project on GitHub, with all the code. You can download a zip with the compiled dll and the views here if you want to

4 thoughts on “Forms and WebForms pt3

  1. Hi Jereon,
    We have this exact same requirement for a client. A few months on from your blog posts, do you have you solution in production? Have you experienced any problems? Just interested to guage whether it is a viable solution in a production environment.
    Many thanks
    Adam

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s