World is now on Opti ID! Learn more

Linus Ekström
Oct 30, 2008
  2668
(0 votes)

Validation of property values

One question that I have got several times is where to do input validation for your custom property since the property architecture in EPiServer CMS 5 requires both class that handles the data and a class that is responsible for the visual presentation of the property. One way this can be done is by adding the actual validation logic before setting the input value from your PropertyControl class:

public class MyCustomProperty : PropertyString
    {
        public override IPropertyControl CreatePropertyControl()
        {
            return new MyCustomPropertyControl();
        }
    }

public class MyCustomPropertyControl : PropertyTextBoxControlBase
{
    public override void ApplyEditChanges()
    {
        string inputValue = EditControl.Text;
        if (inputValue.Length > 10)
        {
            this.AddErrorValidator("The maximum length for " + Name + " must not exceed 10 characters");
            return;
        }
        this.SetValue(inputValue);
    }
}

 

This would result in a validation message that is presented to the editor if he/she would try to enter more than 10 characters in the textbox. So, with just a few rows of code, we have added some validation for the property. But what happens if you try to set the value for the property outside the normal page edit? Or if the user changes the property with on page editing? Or if the data for the property is imported? To handle these cases we need to move the input validation to the PropertyData class:

 

public class MyCustomProperty : PropertyString
{
    public override IPropertyControl CreatePropertyControl()
    {
        return new MyCustomPropertyControl();
    }
    public override object Value
    {
        get
        {
            return base.Value;
        }
        set
        {
            ValidateInput(value as string);
            base.Value = value;
        }
    }

    private void ValidateInput(string value)
    {
        if (!String.IsNullOrEmpty(value) && value.Length > 10)
        {
            throw new Exception("The maximum length for " + Name + " must not exceed 10 characters");
        }
    }
}

public class MyCustomPropertyControl : PropertyTextBoxControlBase
    {
    }

 

This would result in the same behaviour in edit mode but now we have made sure that invalid input can not be entered from other locations as well. In this case you do not even have a need for your own property data control so you might just use one of the built in property controls instead.

 

The base class for property controls, PropertyDataControl, catches the exception and displays it to the editor in the same way as if you call the AddErrorValidator in your property control class. In other places in the ui, like the import dialog, it is also handled in the same way. If you would try to set an invalid value from your own code however, you would have to catch Exceptions and display it to the editor. You could of course also have validation both in the presentation control and in the data control if you for instance want to have a client side validation control in edit mode but still want to be sure that invalid input can not be entered from another location.

Oct 30, 2008

Comments

Please login to comment.
Latest blogs
Make Global Assets Site- and Language-Aware at Indexing Time

I had a support case the other day with a question around search on global assets on a multisite. This is the result of that investigation. This co...

dada | Jun 26, 2025

The remote server returned an error: (400) Bad Request – when configuring Azure Storage for an older Optimizely CMS site

How to fix a strange issue that occurred when I moved editor-uploaded files for some old Optimizely CMS 11 solutions to Azure Storage.

Tomas Hensrud Gulla | Jun 26, 2025 |

Enable Opal AI for your Optimizely products

Learn how to enable Opal AI, and meet your infinite workforce.

Tomas Hensrud Gulla | Jun 25, 2025 |

Deploying to Optimizely Frontend Hosting: A Practical Guide

Optimizely Frontend Hosting is a cloud-based solution for deploying headless frontend applications - currently supporting only Next.js projects. It...

Szymon Uryga | Jun 25, 2025

World on Opti ID

We're excited to announce that world.optimizely.com is now integrated with Opti ID! What does this mean for you? New Users:  You can now log in wit...

Patrick Lam | Jun 22, 2025

Avoid Scandinavian Letters in File Names in Optimizely CMS

Discover how Scandinavian letters in file names can break media in Optimizely CMS—and learn a simple code fix to automatically sanitize uploads for...

Henning Sjørbotten | Jun 19, 2025 |