1
+ using System . ComponentModel . DataAnnotations ;
2
+ using Microsoft . Extensions . DependencyInjection ;
1
3
using Umbraco . Cms . Core . Cache ;
4
+ using Umbraco . Cms . Core . DependencyInjection ;
2
5
using Umbraco . Cms . Core . IO ;
3
6
using Umbraco . Cms . Core . Models ;
4
7
using Umbraco . Cms . Core . Models . Editors ;
@@ -66,7 +69,8 @@ public MediaPicker3PropertyValueEditor(
66
69
ITemporaryFileService temporaryFileService ,
67
70
IScopeProvider scopeProvider ,
68
71
IBackOfficeSecurityAccessor backOfficeSecurityAccessor ,
69
- IDataTypeConfigurationCache dataTypeReadCache )
72
+ IDataTypeConfigurationCache dataTypeReadCache ,
73
+ ILocalizedTextService localizedTextService )
70
74
: base ( shortStringHelper , jsonSerializer , ioHelper , attribute )
71
75
{
72
76
_jsonSerializer = jsonSerializer ;
@@ -76,6 +80,34 @@ public MediaPicker3PropertyValueEditor(
76
80
_scopeProvider = scopeProvider ;
77
81
_backOfficeSecurityAccessor = backOfficeSecurityAccessor ;
78
82
_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
+ {
79
111
}
80
112
81
113
/// <remarks>
@@ -294,5 +326,58 @@ public void ApplyConfiguration(MediaPicker3Configuration? configuration)
294
326
}
295
327
}
296
328
}
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
+ }
297
382
}
298
383
}
0 commit comments