Eric
Mar 28, 2013
  10866
(2 votes)

Use Xform to store data when you have a form with a fixed design

Many of you have probably used xform before and also hate the fact that when the editor create a form inside EPiServer you can not set the layout and design as you like. But xform can be ok to use when store information/data when you have a form that have a specific design and layout or when the editor are not allowed to choose the fields to use.

Since almost every blogpost is about CMS 7 and MVC at the moment I thought this post might help someone still working with CMS 6 or doing ordinary good old EPiServer development Ler

Lately I have been working with a customer that needed a contact form looking something like this:

image

 

They hade some requirements for validation and my part in the project was to create the functionality and store the information. The html/css-part was already made by another developer therefore I could note create css and html to fit the ordinary tags that EPiServer Xform will create when you use it as an editor to produce forms.

But since EPiServer allow use to store information in Xform with the API we can create a form locking exactly like this and with the built-in functionality in EPiServer let the editor collect data. This is very simple. Ler

  1. Create a property of type Xform on the specific PageType
  2. Create an empty form and set the property to use that form

The rest is done by code. First we need to create our form in html. With the help of web controls we can also use some validation. If you do not like to use validation you can delete the web controls handling validation.

   1: <div class="form">
   2:       <div class="form-fields">
   3:           <div class="input">
   4:               <asp:Label runat="server" ID="lblName" AssociatedControlID="txtName">Namn:</asp:Label>
   5:               <asp:TextBox runat="server" ID="txtName" ValidationGroup="form"></asp:TextBox>
   6:               <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtName" ErrorMessage="Vill vill gärna ha ditt namn!" ValidationGroup="form" Display="Dynamic"></asp:RequiredFieldValidator>
   7:           </div>
   8:           <div class="input">
   9:               <asp:Label runat="server" ID="lblMail" AssociatedControlID="txtMail">E-post:</asp:Label>
  10:               <asp:TextBox runat="server" ID="txtMail" ValidationGroup="form"></asp:TextBox>
  11:               <asp:RequiredFieldValidator runat="server" ControlToValidate="txtMail" ValidationGroup="form" ErrorMessage="Fyll i din epost så vi kan kontakta dig!" Display="Dynamic"></asp:RequiredFieldValidator>
  12:               <asp:RegularExpressionValidator runat="server" ValidationExpression=".*@.*\..*" ErrorMessage="Felaktig e-post" ControlToValidate="txtMail" Display="Dynamic" ValidationGroup="form"></asp:RegularExpressionValidator>
  13:           </div>
  14:           <div class="input">
  15:               <asp:Label runat="server" ID="lblCompany" AssociatedControlID="txtCompany">Företag:</asp:Label>
  16:               <asp:TextBox runat="server" ID="txtCompany" ValidationGroup="form"></asp:TextBox>
  17:           </div>
  18:           <div class="input">
  19:               <asp:Label runat="server" ID="lblPhone" AssociatedControlID="txtPhone">Telefonnummer:</asp:Label>
  20:               <asp:TextBox runat="server" ID="txtPhone" ValidationGroup="form"></asp:TextBox>
  21:  
  22:               <asp:RequiredFieldValidator runat="server" ControlToValidate="txtPhone" ValidationGroup="form" ErrorMessage="Du måste ange ett telefonnummer" Display="Dynamic"></asp:RequiredFieldValidator>
  23:               <asp:RegularExpressionValidator runat="server" ValidationExpression="\d*" ErrorMessage="Felaktigt telefonnummer" ControlToValidate="txtPhone" Display="Dynamic" ValidationGroup="form"></asp:RegularExpressionValidator>
  24:  
  25:           </div>
  26:           <div class="input input-message">
  27:               <asp:Label runat="server" ID="Label4" AssociatedControlID="txtMessage">Meddelande:</asp:Label>
  28:               <asp:TextBox runat="server" ID="txtMessage" TextMode="MultiLine" ValidationGroup="form"></asp:TextBox>
  29:           </div>
  30:       </div>
  31:       <div class="clearfix"></div>
  32:       <div class="check">
  33:  
  34:           <label>
  35:               <asp:CheckBox runat="server" ID="chkConfirm" ValidationGroup="form" />
  36:               <span>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec nec hendrerit dolor. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis in est diam, et bibendum mi. Donec molestie nibh sit.</span>
  37:  
  38:  
  39:           </label>
  40:           <asp:CustomValidator ID="CustomValidator1" runat="server" OnServerValidate="ValidateCheckbox" EnableClientScript="True" Display="Dynamic" ValidationGroup="form">Du måste godkänna att vi kontaktar dig.</asp:CustomValidator>
  41:       </div>
  42:       <div class="submit">
  43:           <asp:ImageButton runat="server" ID="btnSubmit" ImageUrl="~/images/foretag/form-skicka.png" PostBackUrl="#contact" ValidationGroup="form" />
  44:       </div>
  45:   </div>

 

The rest is code behind and it is really simple:

   1: if (CurrentPage["Form"] != null && Page.IsValid)
   2: {
   3:     string xformprop = CurrentPage["Form"] as string;
   4:     XForm form = XForm.CreateInstance(new Guid(xformprop));
   5:  
   6:     XFormData formData = form.CreateFormData();
   7:  
   8:     formData.PageGuid = CurrentPage.PageGuid;
   9:     formData.ChannelOptions = ChannelOptions.Database;
  10:  
  11:     //Set values to form
  12:  
  13:     formData.SetValue("Namn", txtName.Text);
  14:     formData.SetValue("E-post", txtMail.Text);
  15:     formData.SetValue("Företag", txtCompany.Text);
  16:     formData.SetValue("Telefonnummer", txtPhone.Text);
  17:     formData.SetValue("Meddelande", txtMessage.Text);
  18:     formData.SetValue("Godkänd", chkConfirm.Checked.ToString());
  19:  
  20:  
  21:     //Save form data to form
  22:     formData.Send();
  23: }

Really nice and easy when you like to store information within a form. Of course you could have use DDS or something like that but since the xform functionality will give the editor control and the capability to export data from the form I think this is ok to use.

In EPiServer CMS 6 the data storage of Xforms and Xform postings is moved to DDS so you could extend your code with searching in forms for instance. You can read more about xforms and dynamic data store in this blogpost by Linus Ekström: http://world.episerver.com/Blogs/Linus-Ekstrom/Dates/2010/7/Using-the-dynamic-data-store-with-XForms/

 

Happy Easter!!

Mar 28, 2013

Comments

Johan Book
Johan Book Apr 1, 2013 11:22 PM

Interesting idea. Never thought about this!

Please login to comment.
Latest blogs
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024