Ram Kumar K
Oct 7, 2018
  7679
(0 votes)

Required Field Validation for Category

We all have come across specific requirements for CMS Content authors. One of such requirement for our news sections is, "Every news article belongs to one or more categories". We must force user to select a category before publish it. We have used a Custom validation rule to fulfill this requirement.

Episerver Documentation: https://world.episerver.com/documentation/developer-guides/CMS/Content/Properties/built-in-property-types/Writing-custom-attributes/

  /// <summary>
     /// Required field validation for Category.
    ///Reference: https://world.episerver.com/documentation/developer-guides/CMS/Content/Properties/built-in-property-types/Writing-custom-attributes/
    /// </summary>

  [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public class FieldRequiredForPublishAttribute : ValidationAttribute
    {
        public bool IsRequired { get; set; }
        public FieldRequiredForPublishAttribute() : this(true) { }
        public FieldRequiredForPublishAttribute(bool isRequired)
        {
            IsRequired = isRequired;
        }

        public override bool IsValid(object value)
        {
            if (value == null) return false;        
            if (value is CategoryList cat) return cat?.Any() == true;          
            return false;
        }

        public override string FormatErrorMessage(string name)
        {
                return $"{name} cannot be empty";
        }
    }

Set RequiredFieldAttribute for News Pages.

        [Display(
              Name = "News Category (*)",
              Description = "Select News Category",
              GroupName = SystemTabNames.Content,
              Order = 3)]
          [FieldRequiredForPublish]
        public override CategoryList Category { get; set; }

 The code is generic enough that you can extend to make any field requied.

Thank you  & happy coding!

Oct 07, 2018

Comments

Stephan Lonntorp
Stephan Lonntorp Oct 8, 2018 07:20 PM

Why not use the built in RequiredAttribute from System.DataAnnotations?

Ram Kumar K
Ram Kumar K Oct 9, 2018 11:43 AM

When you are using "Required" built in functionality, you must enter "required" fields first in the content page. That would chnage the content fields order and may cause confusion. This validation occurs post content creation and before publishing the content. It would let authors save the content and won't publish until validation.

Antti Alasvuo
Antti Alasvuo Oct 9, 2018 06:22 PM

Just in case someone wants to use the RequiredAttribute but not show the fields when creating content then have also a look at Davids blog posting about hiding required properties when creating content. This approach needs less code to achieve the requirement: "don't show property on create content view but have the field required" BUT targeting only CategoryList type currently, so would need some additional code to cover various types.

Please login to comment.
Latest blogs
Optimizely CMS 13 and the Alloy demo site

The Alloy demo site now runs on Optimizely CMS 13. Here’s a quick guide to getting it up and running locally.

Tomas Hensrud Gulla | Apr 10, 2026 |

A day in the life of an Optimizely OMVP: Getting Up to Speed with Optimizely CMS 13 — A Free Learning Path

If you're working with Optimizely and haven't started exploring CMS 13 yet, now's the time. I've put together a dedicated CMS 13 course within the...

Graham Carr | Apr 10, 2026

A day in the life of an Optimizely OMVP: Optimizely CMS 13 Is Here: The Features, The Add-Ons, and What's Coming Next

CMS 13 went GA on 31st March 2026, and after months of previews, webinars, and internal engineering sessions, it's finally real. I've been deep in...

Graham Carr | Apr 10, 2026

How to get the Page Tree back in Optimizely CMS

Lost the page tree in Optimizely CMS? Here's why it happens and the non-obvious fix to get it back.

Henning Sjørbotten | Apr 10, 2026 |