1+ using System . ComponentModel . DataAnnotations ;
2+ using Microsoft . Extensions . DependencyInjection ;
13using Umbraco . Cms . Core . Cache ;
4+ using Umbraco . Cms . Core . DependencyInjection ;
25using Umbraco . Cms . Core . IO ;
36using Umbraco . Cms . Core . Models ;
47using Umbraco . Cms . Core . Models . Editors ;
@@ -66,7 +69,8 @@ public MediaPicker3PropertyValueEditor(
6669 ITemporaryFileService temporaryFileService ,
6770 IScopeProvider scopeProvider ,
6871 IBackOfficeSecurityAccessor backOfficeSecurityAccessor ,
69- IDataTypeConfigurationCache dataTypeReadCache )
72+ IDataTypeConfigurationCache dataTypeReadCache ,
73+ ILocalizedTextService localizedTextService )
7074 : base ( shortStringHelper , jsonSerializer , ioHelper , attribute )
7175 {
7276 _jsonSerializer = jsonSerializer ;
@@ -76,6 +80,34 @@ public MediaPicker3PropertyValueEditor(
7680 _scopeProvider = scopeProvider ;
7781 _backOfficeSecurityAccessor = backOfficeSecurityAccessor ;
7882 _dataTypeReadCache = dataTypeReadCache ;
83+ Validators . Add ( new MinMaxValidator ( jsonSerializer , localizedTextService ) ) ;
84+ }
85+
86+ [ Obsolete ( "Use non obsoleted constructor instead. Scheduled for removal in v17" ) ]
87+ public MediaPicker3PropertyValueEditor (
88+ IShortStringHelper shortStringHelper ,
89+ IJsonSerializer jsonSerializer ,
90+ IIOHelper ioHelper ,
91+ DataEditorAttribute attribute ,
92+ IMediaImportService mediaImportService ,
93+ IMediaService mediaService ,
94+ ITemporaryFileService temporaryFileService ,
95+ IScopeProvider scopeProvider ,
96+ IBackOfficeSecurityAccessor backOfficeSecurityAccessor ,
97+ IDataTypeConfigurationCache dataTypeReadCache )
98+ : this (
99+ shortStringHelper ,
100+ jsonSerializer ,
101+ ioHelper ,
102+ attribute ,
103+ mediaImportService ,
104+ mediaService ,
105+ temporaryFileService ,
106+ scopeProvider ,
107+ backOfficeSecurityAccessor ,
108+ dataTypeReadCache ,
109+ StaticServiceProvider . Instance . GetRequiredService < ILocalizedTextService > ( ) )
110+ {
79111 }
80112
81113 /// <remarks>
@@ -294,5 +326,58 @@ public void ApplyConfiguration(MediaPicker3Configuration? configuration)
294326 }
295327 }
296328 }
329+
330+ private class MinMaxValidator : IValueValidator
331+ {
332+ private readonly IJsonSerializer _jsonSerializer ;
333+ private readonly ILocalizedTextService _localizedTextService ;
334+
335+ public MinMaxValidator ( IJsonSerializer jsonSerializer , ILocalizedTextService localizedTextService )
336+ {
337+ _jsonSerializer = jsonSerializer ;
338+ _localizedTextService = localizedTextService ;
339+ }
340+
341+ public IEnumerable < ValidationResult > Validate ( object ? value , string ? valueType ,
342+ object ? dataTypeConfiguration )
343+ {
344+ var validationResults = new List < ValidationResult > ( ) ;
345+
346+ if ( dataTypeConfiguration is not MediaPicker3Configuration mediaPickerConfiguration )
347+ {
348+ return validationResults ;
349+ }
350+
351+ if ( value is null ||
352+ _jsonSerializer . TryDeserialize ( value , out List < MediaWithCropsDto > ? mediaWithCropsDtos ) is false )
353+ {
354+ return validationResults ;
355+ }
356+
357+ if ( mediaPickerConfiguration . ValidationLimit . Min is not null
358+ && mediaWithCropsDtos . Count < mediaPickerConfiguration . ValidationLimit . Min )
359+ {
360+ validationResults . Add ( new ValidationResult (
361+ _localizedTextService . Localize (
362+ "validation" ,
363+ "entriesShort" ,
364+ new [ ] { mediaPickerConfiguration . ValidationLimit . Min . ToString ( ) , ( mediaPickerConfiguration . ValidationLimit . Min - mediaWithCropsDtos . Count ) . ToString ( ) , } ) ,
365+ new [ ] { "validationLimit" } ) ) ;
366+ }
367+
368+ if ( mediaPickerConfiguration . ValidationLimit . Max is not null
369+ && mediaWithCropsDtos . Count > mediaPickerConfiguration . ValidationLimit . Max )
370+ {
371+ validationResults . Add ( new ValidationResult (
372+ _localizedTextService . Localize (
373+ "validation" ,
374+ "entriesExceed" ,
375+ new [ ] { mediaPickerConfiguration . ValidationLimit . Max . ToString ( ) , ( mediaWithCropsDtos . Count - mediaPickerConfiguration . ValidationLimit . Max ) . ToString ( ) , } ) ,
376+ new [ ] { "validationLimit" } ) ) ;
377+ }
378+
379+ return validationResults ;
380+ }
381+ }
297382 }
298383}
0 commit comments