We have been working with Kentico and their BizForms module for a while now to implement “simple” forms to capture data. But like any work you do that uses the word simple, it quickly becomes a complex form with conditional validation. Revium uses custom form controls to extend Kentico BizForms.

So we have been working with Kentico and their BizForms module for a while now to implement “simple” forms to capture data. But like any work you do that uses the word simple, it quickly becomes a complex form with conditional validation.

Most examples out there allow you to handle custom validation by creating a new BizForm and handling the OnAfterValidate event. This works in simple cases (there’s that word again). But if you need to perform some: if value X is selected in control A then control B is required, it just isn’t that simple.

To enable us to handle this we we need to do 2 things:

  1. Create a custom BizForm WebPart
  2. Create a custom TextBox FormControl

The example listed below is used is the situation where you have a drop down list called cars with the value “other”. When “other” is selected, a textbox for cars_other is required.

Custom TextBox FormControl
The custom TextBox FormControl is required so that we can raise an IsValid event to validate the field. This way in our BizForm we can create custom field validation function to handle each event.

To create this control we take a copy of the existing TextBoxControl from \CMSFormControls\Basic\ and put it into our sites FormControls folder \ReviumSite\FormControls\
After modifying the class names in both the .ascx and .cs files we need to make the following code changes


public partial class ReviumSite_FormControls_TextBoxControl  : FormEngineUserControl
{
    //Add a public event
    public event EventHandler OnIsValid;
 
    //...
 
    //Modify IsValid to raise our event if the control is valid (if it is invalid an error is going to be showed anyway)
    public override bool IsValid()
    {
        bool IsValidValue = false;
 
        if (IsTextArea)
        {
            int maxControlSize = MaxLength;
            int minControlSize = 0;
            string error = null;
 
            // Check min/max text length
            if (FieldInfo != null)
            {
                if ((FieldInfo.MaxStringLength > -1) && ((maxControlSize > FieldInfo.MaxStringLength) || (maxControlSize == 0)))
                {
                    maxControlSize = FieldInfo.MaxStringLength;
                }
                if ((FieldInfo.Size > 0) && ((maxControlSize > FieldInfo.Size) || (maxControlSize == 0)))
                {
                    maxControlSize = FieldInfo.Size;
                }
                minControlSize = FieldInfo.MinStringLength;
            }
 
            // Get text length
            int textLength = textbox.Text.Length;
            if (Trim)
            {
                // Get trimmed text length
                textLength = textbox.Text.Trim().Length;
            }
 
            bool valid = CheckLength(minControlSize, maxControlSize, textLength, ref error, ErrorMessage);
            ValidationError = error;
 
            IsValidValue = valid;
        }
        else
        {
            IsValidValue = true;
        }
 
        if (IsValidValue && OnIsValid != null)
            OnIsValid(this, new EventArgs()); //When this event is handled we can set IsValid to false is there is an error
 
        return IsValidValue;
    }

We can now add this control to Kentico. From Site Manager > Development > Form Controls add a new form control. Copy all the properties from the default Text Box control (there are a few of them). In the File name property select our new textbox .ascx file: ~/ReviumSite/FormControls/TextBoxControl.ascx

Custom BizForm Webpart
The custom BizForm WebPart is required to implement the custom validation.

To create this control we take a copy of the existing BizForm from \CMSWebParts\BizForms\ and put it into our sites WebParts folder \ReviumSite\WebParts\
After modifying the class names in both the .ascx and .cs files we need to make the following code changes


protected void SetupControl()
{
    //...
 
    //Add an event handler to handle our events
    ViewBiz.OnBeforeValidate += ViewBiz_OnBeforeValidate;
 
    //...
}
 
//In this function add a switch statement so we can apply validation to specific forms
void ViewBiz_OnBeforeValidate(object sender, EventArgs e)
{
    switch (BizFormName)
    {
        case "FormOne":
            FormOne_OnBeforeValidate();
            break;
 
        case "FormTwo":
            //FormTwo_OnBeforeValidate();
            break;
    }
}
 
#region FormOneEvents
 
private void FormOne_OnBeforeValidate()
{
    //Attach Event Handlers for custom validaiton
    var cars_other = ViewBiz.BasicForm.FieldControls["cars_other"] as ReviumSite_FormControls_TextBoxControl;
    cars_other.OnIsValid += new EventHandler(FormOne_CarsOther_OnIsValid);
}
 
protected void FormOne_CarsOther_OnIsValid(object sender, EventArgs e)
{
    ReviumSite_FormControls_TextBoxControl cars_other = (ReviumSite_FormControls_TextBoxControl)sender;
    var cars = ViewBiz.BasicForm.FieldEditingControls["Cars"] as EditingFormControl;
 
    cars_other.IsValidValue = true;
 
    if (cars.Value.ToString().ToLower() == "other" && string.IsNullOrEmpty(cars_other.Text))
        cars_other.IsValidValue = false;
}
 
#endregion

Now we can add this web part to Kentico. From Site Manager > Development > Web parts > add a new web part. Copy all the properties from the default BizForm web part. In the File name property select our new textbox .ascx file: ~/ReviumSite/WebParts/BizForm.ascx

Creating your form

  • Create a new form
  • Add Cars field that uses a dropdown list. (one of the values must be: other)
  • Add an Other field that uses the Revium Textbox Control

Using our new form on a page

  • Edit a page
  • Click Insert/Edit widget
  • Choose the Revium On-line form
  • Select the form we just created

Now you can view the live site and test the validation.
This technique can obviously be applied to other controls as well and you don’t have to make copies of the controls if you don’t want. We prefer to do it this way so any updates wont override our business logic and hard work!

You may also like

Linq – Orderby calculated value

I just came across a small problem where i had a table with 2 priority columns. One is generated by the system and the other enabled users to manually override the generated priority. The requirement was to sort the data by the highest of the 2 columns. The problem is the manual priority might be null.

Keep Reading

Creating a generic settings repository in C#

Every project has a group of settings that can be used to configure a web site. They can be stored in the web.config file or in a database. Typically what happens is you create a number of functions to read settings like...

Keep Reading

Newsletter sign up

Every couple of months we send out an update on what's been happening around our office and the web. Sign up and see what you think. And of course, we never spam.