Files
Umbraco-CMS/src/Umbraco.Infrastructure/PropertyEditors/Validators/RichTextRequiredValidator.cs
Bjarke Berg bafdb21b45 Fix mandatory RTE validation (#16962)
* Added a custom RichTextRequiredValidator, to check that the empty richtext object (still with json) can be required or not. We are now testing the markdown needs to have a value

* Fixed namespaced and moved back wrong class

* Cleanup
2024-08-26 15:03:11 +02:00

31 lines
1.1 KiB
C#

using System.ComponentModel.DataAnnotations;
using Microsoft.Extensions.Logging;
using Umbraco.Cms.Core.Serialization;
using Umbraco.Cms.Core.Services;
namespace Umbraco.Cms.Core.PropertyEditors.Validators;
internal class RichTextRequiredValidator : RequiredValidator, IRichTextRequiredValidator
{
private readonly IJsonSerializer _jsonSerializer;
private readonly ILogger<RichTextRequiredValidator> _logger;
public RichTextRequiredValidator(ILocalizedTextService textService, IJsonSerializer jsonSerializer, ILogger<RichTextRequiredValidator> logger) : base(textService)
{
_jsonSerializer = jsonSerializer;
_logger = logger;
}
public override IEnumerable<ValidationResult> ValidateRequired(object? value, string? valueType) => base.ValidateRequired(GetValue(value), valueType);
private object? GetValue(object? value)
{
if(RichTextPropertyEditorHelper.TryParseRichTextEditorValue(value, _jsonSerializer, _logger, out RichTextEditorValue? richTextEditorValue))
{
return richTextEditorValue?.Markup;
}
return value;
}
}