Skip to content

Commit

Permalink
Merge pull request #17762 from umbraco/v15/task/media-picker-min-max-…
Browse files Browse the repository at this point in the history
…validation

V15 min/max validator for mediapicker
  • Loading branch information
bergmania authored Dec 13, 2024
2 parents cd25c9a + ddeb253 commit 0b9855c
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using System.ComponentModel.DataAnnotations;
using Microsoft.Extensions.DependencyInjection;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.IO;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.Editors;
using Umbraco.Cms.Core.Models.TemporaryFile;
using Umbraco.Cms.Core.Models.Validation;
using Umbraco.Cms.Core.PropertyEditors.ValueConverters;
using Umbraco.Cms.Core.Security;
using Umbraco.Cms.Core.Serialization;
Expand Down Expand Up @@ -66,7 +70,8 @@ public MediaPicker3PropertyValueEditor(
ITemporaryFileService temporaryFileService,
IScopeProvider scopeProvider,
IBackOfficeSecurityAccessor backOfficeSecurityAccessor,
IDataTypeConfigurationCache dataTypeReadCache)
IDataTypeConfigurationCache dataTypeReadCache,
ILocalizedTextService localizedTextService)
: base(shortStringHelper, jsonSerializer, ioHelper, attribute)
{
_jsonSerializer = jsonSerializer;
Expand All @@ -76,6 +81,34 @@ public MediaPicker3PropertyValueEditor(
_scopeProvider = scopeProvider;
_backOfficeSecurityAccessor = backOfficeSecurityAccessor;
_dataTypeReadCache = dataTypeReadCache;
Validators.Add(new MinMaxValidator(jsonSerializer, localizedTextService));
}

[Obsolete("Use non obsoleted constructor instead. Scheduled for removal in v17")]
public MediaPicker3PropertyValueEditor(
IShortStringHelper shortStringHelper,
IJsonSerializer jsonSerializer,
IIOHelper ioHelper,
DataEditorAttribute attribute,
IMediaImportService mediaImportService,
IMediaService mediaService,
ITemporaryFileService temporaryFileService,
IScopeProvider scopeProvider,
IBackOfficeSecurityAccessor backOfficeSecurityAccessor,
IDataTypeConfigurationCache dataTypeReadCache)
: this(
shortStringHelper,
jsonSerializer,
ioHelper,
attribute,
mediaImportService,
mediaService,
temporaryFileService,
scopeProvider,
backOfficeSecurityAccessor,
dataTypeReadCache,
StaticServiceProvider.Instance.GetRequiredService<ILocalizedTextService>())
{
}

/// <remarks>
Expand Down Expand Up @@ -294,5 +327,61 @@ public void ApplyConfiguration(MediaPicker3Configuration? configuration)
}
}
}

private class MinMaxValidator : IValueValidator
{
private readonly IJsonSerializer _jsonSerializer;
private readonly ILocalizedTextService _localizedTextService;

public MinMaxValidator(IJsonSerializer jsonSerializer, ILocalizedTextService localizedTextService)
{
_jsonSerializer = jsonSerializer;
_localizedTextService = localizedTextService;
}

public IEnumerable<ValidationResult> Validate(
object? value,
string? valueType,
object? dataTypeConfiguration,
PropertyValidationContext validationContext)
{
var validationResults = new List<ValidationResult>();

if (dataTypeConfiguration is not MediaPicker3Configuration mediaPickerConfiguration)
{
return validationResults;
}

if (value is null ||
_jsonSerializer.TryDeserialize(value, out List<MediaWithCropsDto>? mediaWithCropsDtos) is false)
{
return validationResults;
}

if (mediaPickerConfiguration.ValidationLimit.Min is not null
&& mediaWithCropsDtos.Count < mediaPickerConfiguration.ValidationLimit.Min)
{
validationResults.Add(new ValidationResult(
_localizedTextService.Localize(
"validation",
"entriesShort",
new[] { mediaPickerConfiguration.ValidationLimit.Min.ToString(), (mediaPickerConfiguration.ValidationLimit.Min - mediaWithCropsDtos.Count).ToString(), }),
new[] { "validationLimit" }));
}

if (mediaPickerConfiguration.ValidationLimit.Max is not null
&& mediaWithCropsDtos.Count > mediaPickerConfiguration.ValidationLimit.Max)
{
validationResults.Add(new ValidationResult(
_localizedTextService.Localize(
"validation",
"entriesExceed",
new[] { mediaPickerConfiguration.ValidationLimit.Max.ToString(), (mediaWithCropsDtos.Count - mediaPickerConfiguration.ValidationLimit.Max).ToString(), }),
new[] { "validationLimit" }));
}

return validationResults;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public class DataValueReferenceFactoryCollectionTests
Mock.Of<ITemporaryFileService>(),
Mock.Of<IScopeProvider>(),
Mock.Of<IBackOfficeSecurityAccessor>(),
Mock.Of<IDataTypeConfigurationCache>()));
Mock.Of<IDataTypeConfigurationCache>(),
Mock.Of<ILocalizedTextService>()));

private IIOHelper IOHelper { get; } = Mock.Of<IIOHelper>();

Expand Down

0 comments on commit 0b9855c

Please sign in to comment.