In this tutorial, you will learn how to add validation to an Episerver content area. This validation will allow you to restrict what blocks content editors can add into the CMS. A big part of any Episerver solution architect duties is designing a backend that is easy to use. In a well-designed system, content editors should not be allowed to add blocks into pages where they have not been designed or tested to be used. As of Episerver 8 adding this type of restriction is easy using the [AllowedTypes]
. If you want to learn how to use this attribute, read on 🔥🔥🔥
AllowedTypes
The [AllowedTypes]
attribute is a new Episerver 8 attribute that gives a lot more flexibility in restricting what can be done within the editor. Some common use-cases for the [AllowedTypes]
attribute are:
- Define what block types are allowed to be added into a content area
- Restrict a subset of blocks that are not allowed
- Add validation using interfaces
The code to implement the [AllowedTypes]
attribute is shown below:
All you need to do is decorate the Episerver property you want to restrict with AllowedTypes
and pass in the types you want to allow 💥
AvailableContentTypes
If you are using Episerver 7.5 and below, you will be stuck with either using the [AvailableContentTypes]
attribute or implementing a custom solution. An example of how to use the [AvailableContentTypes]
attribute is shown below:
Custom Validation
It is also possible to create a custom validation using either ValidationAttribute
or the IValidate
interface. An example of a custom validator that manually checks the block type is shown below:
If you want to learn more about custom validation, I recommend reading this or this
You now have the power to restrict blocks as you need. Happy Coding 🤘