Linus Ekström
Oct 30, 2008
  4611
(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

Oct 12, 2010 10:33 AM

Havnt been working with EPiServer since 4.x, and i guess the issue still applies. Why aint there any properties settings for the property themself? For instances setting max-lenght or format-pattern to a property. Then we could reuse many more properties and not need some speisialized config file to achive the same ting.
/ Kristian

Oct 12, 2010 10:33 AM

Hi! The main reason for this was, as with so many other features, just a lack of time. I like the general idea that you attach validation and settings to a property to be able to reuse them and I hope that we will be able to implement this to the next major version of EPiServer.
/ Linus Ekström (linuse)

Oct 12, 2010 10:33 AM

Good to hear. A general settings to properties (for a pagetype) would be great. And would surly help with lame properties names as "lastname_maxlenght_200" :)
/ Kristian

Oct 12, 2010 10:33 AM

I have a problem with this method when the property is used in dynamic content. The property gets validated the same time dynamic content window pops up. It also shows the error twice. Is there a different aproach to validate custom properties in dynamic content?
/ Dmitri

Oct 12, 2010 10:33 AM

Hello Dimitri, I have tried to reproduce your problem in dynamic content. But I am not sure how you use it in dynamic content. Could you post some more info and maybe move the question to the forum?
/ Johan Sellberg

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