Skip to content

Mandatory flag on a Content Blocks property is silently ignored #104

Description

@talhamalik4025

Describe the bug

Ticking Mandatory on a property using the Perplex.ContentBlocks editor never produces a validation error, however empty the property is. The property saves and publishes with zero blocks.

Steps to reproduce

  1. Add a Perplex.ContentBlocks property to a document type and tick Mandatory.
  2. Create a page and add no blocks.
  3. Save and publish — it succeeds, with no validation message on the property.

Cause

Umbraco's RequiredValidator treats a JSON-typed value as empty only when it is null or when the string collapses to {} or [] (StringExtensions.JsonEmpties). An untouched Content Blocks property is a versioned envelope — {"version":3,"header":null,"blocks":[]} — which matches neither, so the required check always passes.

ContentBlocksValueEditor does not override DataValueEditor.RequiredValidator, and ContentBlocksValidator only walks the properties inside each existing block, so with zero blocks it yields no validation models.

For comparison, Umbraco's own Block List keeps the contract client-side: umbBlockListPropertyEditor.component.js sets vm.model.value = {} when the value is not an object, which is exactly what JsonEmpties matches, so Mandatory works there.

Verified on Umbraco 13.14.0 with Perplex.ContentBlocks 3.0.1. RequiredValidator is unchanged on Umbraco's v17 branch, and on this repo's current main ContentBlocksValueEditor still overrides only ToEditor/FromEditor, so 4.x looks the same.

Suggested fix

Either collapse an empty value to {} as the core block editors do, or override RequiredValidator on ContentBlocksValueEditor. Umbraco has a precedent for the second in RichTextRequiredValidator, which normalises the value and defers to the base:

public override IEnumerable<ValidationResult> ValidateRequired(object? value, string? valueType)
    => base.ValidateRequired(IsEmptyEnvelope(value) ? "{}" : value, valueType);

Since the configuration editor exposes no minimum-blocks setting, Mandatory is currently the only way to require content in the property.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions