When Either of Two Kentico Fields Is Required

Editorial illustration.

Originally published 16 June 2023; rewritten and reviewed on 15 October 2026.

A common form requirement looks simple until you try to wire it up in a CMS: the user can provide either a product code or a product description. Marking both fields as required is wrong because a user who only knows the code gets blocked. Leaving both optional is equally wrong because someone can submit a completely blank request.

The validation rule does not belong to ProductCode or ProductDescription individually. It belongs to the pair.

State the rule in ordinary language first

Before touching Kentico’s form builder or writing any code, clarify whether the business rule is inclusive OR or exclusive OR:

  • At least one must be filled (inclusive OR): both empty fails; one filled passes; both filled also passes.
  • Exactly one must be filled (exclusive OR): both empty fails; both filled fails; only one filled passes.

Most support and enquiry forms want the first version. In C#, that rule is one readable expression:

bool hasProductReference =
    !string.IsNullOrWhiteSpace(productCode) ||
    !string.IsNullOrWhiteSpace(productDescription);

Using string.IsNullOrWhiteSpace matters more than it looks. If you only check string.IsNullOrEmpty, a user—or an automated browser test—can slip past validation by typing a couple of spaces.

Place validation at the supported server boundary

The original 2023 walkthrough demonstrated this using Kentico’s Portal Engine form and macro-rule configuration. Because Kentico deployments span older Web Forms / Portal Engine sites and newer ASP.NET Core MVC (Xperience) projects, the exact UI screen or hook depends on the version you maintain.

What stays constant is where the truth lives: always enforce the rule on the server. Client-side JavaScript is helpful for fast feedback, but anyone can bypass it with curl, a modified request payload, or a browser with scripts disabled.

In an ASP.NET Core MVC model or custom form model, IValidatableObject is a clean way to express a cross-field rule in one place:

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

public class ProductEnquiryViewModel : IValidatableObject
{
    public string? ProductCode { get; set; }
    public string? ProductDescription { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (string.IsNullOrWhiteSpace(ProductCode) &&
            string.IsNullOrWhiteSpace(ProductDescription))
        {
            yield return new ValidationResult(
                "Enter either a product code or a product description.",
                new[] { nameof(ProductCode), nameof(ProductDescription) });
        }
    }
}

If you are maintaining a legacy Kentico Portal Engine BizForm, the equivalent boundary is the form’s server-side validation event (OnBeforeValidate) or a custom validation macro condition evaluated before the record is saved. Avoid hiding the rule inside a button’s OnClick handler alone, where partial postbacks or alternate submit paths can miss it.

Explain the correction without blaming one field

A common UX mistake with paired validation is attaching an error message like “This field is required” solely to ProductCode. A user who intended to type a description sees a red border on the code box and wonders why the form is forcing them to invent a SKU they do not have.

Instead, phrase the message so it explains the relationship: “Enter a product code or a product description.” Display it in a shared validation summary or above the paired inputs, and keep aria-describedby linked so screen-reader users hear the same instruction. Just as importantly, preserve whatever valid values the user already entered in the rest of the form so a validation error never wipes their work.

Test the four-case truth table

Cross-field rules are quick to verify once you write down the four combinations plus whitespace:

  1. Both ProductCode and ProductDescription empty -> fails with the shared message.
  2. Whitespace only (" ") in one or both fields -> fails (normalised before checking).
  3. ProductCode filled, ProductDescription empty -> passes.
  4. ProductCode empty, ProductDescription filled -> passes.
  5. Both filled -> passes (or fails, if your spec explicitly calls for exclusive OR).

A reliable form does more than reject bad payloads at the database door. It states the rule clearly, validates it at the server boundary, and gives the person filling it out enough context to fix the submission on the first try.

Images from the original article

The following images are retained as historical context; interfaces and configuration options may have changed.

Original article image 1: When Either of Two Kentico Fields Is Required

Original article image 2: When Either of Two Kentico Fields Is Required

Original article image 3: When Either of Two Kentico Fields Is Required

Original article image 4: When Either of Two Kentico Fields Is Required

Original article image 5: When Either of Two Kentico Fields Is Required