Forms and WebForms pt2

The POC continues. As you may have noticed the solution I wrote about yesterday only worked sever side, no client side validation e.g.

This can be fixed as well. First I added another method to my util class, to get the fake ControllerContext and refactored it, so you won’t need the Dummy controller. I just use the FormContainerController: You will need that later.

public static ControllerContext GetFormControllerContext()
        {
            try
            {
                HttpContextBase httpContextBase = new HttpContextWrapper(HttpContext.Current);
                RouteData routeData = new RouteData();
                routeData.Values.Add("controller", "FormContainerBlock");
                routeData.Values.Add("action", "Index");
                return new ControllerContext(new RequestContext(httpContextBase, routeData), new FormContainerBlockController());
            }
            catch (ArgumentNullException argumentNullException)
            {
                Log.Critical("Cannot create controllercontext for the FormContainerBlock: \r\n {0}", argumentNullException);
            }

            return null;
        }

Next I added the method from the controller to register the resources to my UserControl, the complete code is in Gist:

public partial class FormContainerBlockControl : BlockControlBase<FormContainerBlock>
    {
        protected ContentArea FakeArea { get; private set; }

        protected ControllerContext FakeContext { get; private set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.FakeContext = MvcUtility.GetFormControllerContext();

            this.SetResources(this.FakeContext);

            // this.Page.Form.ID = this.CurrentBlock.Form.FormGuid.ToString();
            if (!PageEditing.PageIsInEditMode)
            {
                GhostForm form = this.Page.Form as GhostForm;

                    if (form != null)
                    {
                        form.RenderFormTag = false;
                    }
            }

            ContentArea contentArea = new ContentArea();
            contentArea.Items.Add(new ContentAreaItem { ContentLink = this.CurrentBlock.Content.ContentLink });

            this.FakeArea = contentArea;
            this.DataBind();
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            this.SetVisitorIdentifierIfNeeded(this.FakeContext.HttpContext);
        }       
    }

Still no luck though. Because you are using WebForms you have two forms, which confuses the module. So I  somethingto hide the WebForms form when there is an EPiServer form on the page. I hope the Forms team will add an option to disable rendering the form tag. That way you can just set some attributes, instead of hiding it. So a custom control. You will need to replace the form control on your page with this custom one, thanks Jeremy

public class GhostForm : HtmlForm
    {
        protected bool render;

        public bool RenderFormTag
        {
            get { return this.render; }
            set { this.render = value; }
        }

        public GhostForm()
        {
            //By default, show the form tag
            this.render = true;
        }

        protected override void RenderBeginTag(HtmlTextWriter writer)
        {
            //Only render the tag when render is set to true
            if (this.render)
            {
                base.RenderBeginTag(writer);
            }
        }

        protected override void RenderEndTag(HtmlTextWriter writer)
        {
            //Only render the tag when render is set to true
            if (this.render)
            {
                base.RenderEndTag(writer);
            }
        }
    }

You see in the UserControl that I hide the WebForm when the control is on a page.

After this hack clientside validation was working, the default css was there. Yay. And the forms were submitted and the data could be retrieved.

 

This feels more hackish than what I did yesterday. So I’ll ask the forms team if they would be willing to make the rendering of the EPiServer Forms tag optional/disable it with a setting. That way you could do something like this in your masterpage:

<form runat="server" enctype="multipart/form-data" class="EPiServerForms " data-epiforms-type="form" ClientIDMode="Static">

And in the UserControl

this.Page.Form.ID = this.CurrentBlock.Form.FormGuid.ToString();

I will also ask them to make the setting of the resources that is done in the controller a separate method we can use.

So another step further and more testing to do.

All updated code en new code is on gist.

I have put all code on GitHub, you can also download a zip with the compiled dll and the views.

The POC continues in pt3

Advertisements

4 thoughts on “Forms and WebForms pt2

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