How To Return Different Valdation Warnings To Content Editors When Publishing In Episerver

In Episerver 7+ developers have been provided with a handy new feature to provide different levels of feedback to content editors when they create new pages and blocks using the ValidationErrorSeverity enumeration. The ValidationErrorSeverity comes in four flavours:

  • None
  • Info
  • Warning
  • Error

Which, when triggered, a dialogue will appear next to the publish button:

ValidationErrorSeverity

There are two ways of applying validation to page types and properties in Episerver:

  • ValidationAttribute
  • IValidate

In the example above, only IValidate let’s you use the warning and info states, so, we will start there.

If you are interested in the ValidationAttribute I have written more about it in, How to Create a Custom Content Area Validator in Episerver?

IValidate

The EPiServer.Validation.IValidate interface was introduced in Episerver 7+. When your website loads for the first time, EPiServer scans all your assemblies. One of the big reasons for doing this is to find all pages and blocks that have been defined in your solution. It also scans for any classes that implements IValidate. If it finds a match it will register the validator against the page type and the classes Validate() method will then be called whenever the type is saved in the editor.

Creating a validator takes minimal code and the example from the screenshot above can be seen below:

public class ContentPageValidator : IValidate<ContentPage>
{
IEnumerable<ValidationError> IValidate<ContentPage>.Validate(ContentPage page)
{
// We can do logic on the pages properties here
return new[]
{
new ValidationError()
{
ErrorMessage = "This is information",
PropertyName = page.GetPropertyName(property => property.PageName),
Severity = ValidationErrorSeverity.Info,
ValidationType = ValidationErrorType.AttributeMatched
},
new ValidationError()
{
ErrorMessage = "This is a warning",
PropertyName = page.GetPropertyName(property => property .PageName),
Severity = ValidationErrorSeverity.Warning,
ValidationType = ValidationErrorType.AttributeMatched
},
new ValidationError()
{
ErrorMessage = "This is an error",
PropertyName = page.GetPropertyName(property => property .PageName),
Severity = ValidationErrorSeverity.Error,
ValidationType = ValidationErrorType.AttributeMatched
}
};
}

In your execute method you get the strongly typed object of the ‘thing’ you are trying to validate. In here you can then perform any validation on the properties you wish.

If you find an error you can then return a ValidationError setting the Severity property to the level you want to. The text added into the ErrorMessage property is the text that will be displayed back to the content editor.

Conclusion

IN today’s tutorial we’ve covered how you can display validation messages to content editors, using the ValidationErrorSeverity enumeration. We have two ways of validating pages and properties, IValidate for validating pages themselves and ValidationAttribute to apply validation to properties.

In today’s guide, we’ve covered all the code required to validate a page in our solution called ‘ContentPage’ and return a variety of validation message back to the user.

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

More Posts

1 reply

Trackbacks & Pingbacks

  1. […] talked previously about things like, How To Sort The Tab Order In Your EpiServer Pages Or Blocks, How To Return Different Valdation Warnings To Content Editors When Publishing In EpiServer and Defining The Available Page Types Allowed To Be Created Under a Page in EpiServer to name a […]

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *