Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Jacob Khan
May 31, 2010
  12253
(0 votes)

XForms validation

I recently got a question from a partner if it is possible to add validation to check if two fields are equal to each other. The scenario was that a user is asked for their email address and then asked to confirm it. I have already blogged about how to add a simple regular expression validation but this requires a bit more code but not much. Anders Hattestad wrote a great blog post on how to extend XForms and with the help of his blog post I got my validation working.

I start by adding a regular expression validation in my global.asax.

   1: protected void Application_Start(Object sender, EventArgs e)
   2:         {
   3:             EPiServer.XForms.DataTypes.Types.Add("Similar", "aretheysimilar");
   4:             XFormControl.ControlSetup += new EventHandler(XForm_ControlSetup);
   5:         }

This is only done to get the validation to show up in our drop down list.xformssim

When I select two different textboxes I mark both as similar in the drop down list.

On the event control.ControlsCreated += I attach and add some code.

First thing I do is that I try to find the regular expression validation added with my dummy regex.

 

   1: List<string> equalcontrolstocheck = new List<string>();
   2:           List<string> removecontrols = new List<string>();
   3:           foreach (Control item in  formControl.Controls)
   4:           {
   5:               if (item is System.Web.UI.WebControls.RegularExpressionValidator)
   6:               {
   7:                   if (((RegularExpressionValidator)item).ValidationExpression == "aretheysimilar")
   8:                   {
   9:                       Control c = formControl.FindControl(((RegularExpressionValidator)item).ControlToValidate);
  10:  
  11:                       equalcontrolstocheck.Add(c.ID);
  12:                       removecontrols.Add(item.ID);
  13:                   }
  14:               }
  15:           }

I then add the controls to a list and the controls they validate. I then remove the regular expression validation controls and add a compare control saying that both controls should be equal

 

   1: if (equalcontrolstocheck.Count > 1)
   2:           {
   3:  
   4:               formControl.Controls.Remove(formControl.FindControl(removecontrols[0]));
   5:               formControl.Controls.Remove(formControl.FindControl(removecontrols[1]));
   6:  
   7:               CompareValidator comparevalid = new CompareValidator();
   8:               comparevalid.ControlToValidate = equalcontrolstocheck[0];
   9:               comparevalid.ControlToCompare = equalcontrolstocheck[1];
  10:               comparevalid.Type = ValidationDataType.String;
  11:               comparevalid.Operator = ValidationCompareOperator.Equal;
  12:               comparevalid.Text = "They do not match";
  13:               comparevalid.ID = "emailisequal";
  14:               comparevalid.Display = ValidatorDisplay.Dynamic;
  15:               comparevalid.ValidationGroup = "XForm";
  16:  
  17:               formControl.Controls.Add(comparevalid);
  18:  
  19:           }

The end results are that if they are not equal it will show

 

similar

May 31, 2010

Comments

Please login to comment.
Latest blogs
Optimizely Unit Testing Using CmsContentScaffolding Package

Introduction Unit tests shouldn't be created just for business logic, but also for the content and rules defined for content creation (available...

MilosR | Apr 26, 2024

Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024

Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog