Skip to content

Commit 936ffd7

Browse files
authored
Merge pull request #8 from rasmuseeg/feature/custom-dictionary-keys
Feature/custom dictionary keys
2 parents 759880e + 25d83ed commit 936ffd7

File tree

89 files changed

+3241
-78
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+3241
-78
lines changed

.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,4 +258,10 @@ paket-files/
258258

259259
# Python Tools for Visual Studio (PTVS)
260260
__pycache__/
261-
*.pyc
261+
*.pyc
262+
263+
# UmbracoCms
264+
umbraco/
265+
umbraco_client/
266+
App_Data/
267+
App_Code

README.md

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,7 @@ public string Username { get; set; }
150150

151151
Example:
152152
```c#
153-
[UmbracoRequired]
154-
[UmbracoEmailAddress]
155-
[UmbracoDisplayName(nameof(Email))]
156-
[DataType(DataType.EmailAddress)]
153+
[UmbracoEmailAddress(DictionaryKey = "MyCustomKey")]
157154
public string Email { get; set; }
158155
```
159156

@@ -165,7 +162,7 @@ public string Email { get; set; }
165162

166163
Example:
167164
```C#
168-
[UmbracoMinLength(20)]
165+
[UmbracoMinLength(20, DictionaryKey = "MyCustomKey")]
169166
property string MyProperty { get; set; }
170167
```
171168

@@ -177,7 +174,7 @@ property string MyProperty { get; set; }
177174

178175
Example:
179176
```C#
180-
[UmbracoMaxLength(120)]
177+
[UmbracoMaxLength(120, DictionaryKey = "MyCustomKey")]
181178
property string MyProperty { get; set; }
182179
```
183180

@@ -190,7 +187,7 @@ property string MyProperty { get; set; }
190187

191188
Example:
192189
```C#
193-
[UmbracoStringLength(120, MinimumLength = 30)]
190+
[UmbracoStringLength(120, MinimumLength = 30, DictionaryKey = "MyCustomKey")]
194191
property string Message { get; set; }
195192
```
196193

@@ -201,7 +198,7 @@ property string Message { get; set; }
201198

202199
Example:
203200
```C#
204-
[MustBeTrue]
201+
[UmbracoMustBeTrue(DictionaryKey = "MyCustomKey")]
205202
property boool Consent { get; set; }
206203
```
207204

@@ -215,7 +212,11 @@ property boool Consent { get; set; }
215212

216213
Example:
217214
```C#
218-
[UmbracoPassword]
215+
[UmbracoPassword(DictionaryKey = "CustomPasswordKey",
216+
MinPasswordLengthDictionaryKey = "CustomMinPasswordLengthKey",
217+
MinNonAlphanumericCharactersDictionaryKey = "MyCustomMinNonAlphanumericCharactersKey",
218+
PasswordStrengthDictionaryKey = "MyCustomPasswordStrengtKey",
219+
PasswordStrengthRegexTimeout = 360)]
219220
property string Password { get; set; }
220221
```
221222

@@ -225,14 +226,25 @@ There are no default keys for this attribute, since each regex validation is uni
225226

226227
Example:
227228
```C#
228-
[UmbracoRegularExpression()]
229+
[UmbracoRegularExpression(DictionaryKey = "MyCustomRegexKey")]
229230
property string Password { get; set; }
230231
```
231232

232233
### UmbracoRequired
233234

234235
Example:
235236
```C#
236-
[UmbracoRequired]
237+
[UmbracoRequired(DictionaryKey = "MyCustomRequiredKey")]
238+
property string MyProperty { get; set; }
239+
```
240+
241+
## Custom dictionary keys
242+
Each Attribute, has a public property `DictionaryKey` which can be set like this:
243+
```
244+
[UmbracoReguired(DictionaryKey = "MyCustomKey")]
245+
[UmbracoRegularExpression(DictionaryKey = "MyCustomRegexKey")]
246+
[UmbracoRegularExpression(DictionaryKey = "MyCustomRegexKey")]
237247
property string MyProperty { get; set; }
238-
```
248+
```
249+
250+
Not setting a custom key, will fallback to the default dictionary key.

src/Our.Umbraco.DataAnnotations.sln

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
VisualStudioVersion = 15.0.28010.2048
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29403.142
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Our.Umbraco.DataAnnotations", "Our.Umbraco.DataAnnotations\Our.Umbraco.DataAnnotations.csproj", "{C808941C-0685-4F06-80CE-81AE4E8B82E3}"
77
EndProject
@@ -19,6 +19,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{041121B7
1919
Our.Umbraco.DataAnnotations\Our.Umbraco.DataAnnotations.nuspec = Our.Umbraco.DataAnnotations\Our.Umbraco.DataAnnotations.nuspec
2020
EndProjectSection
2121
EndProject
22+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UmbracoCms", "UmbracoCms\UmbracoCms.csproj", "{03A5C03C-2BF9-4EF5-9473-867F2D31BFC6}"
23+
EndProject
2224
Global
2325
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2426
Debug|Any CPU = Debug|Any CPU
@@ -29,6 +31,10 @@ Global
2931
{C808941C-0685-4F06-80CE-81AE4E8B82E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
3032
{C808941C-0685-4F06-80CE-81AE4E8B82E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
3133
{C808941C-0685-4F06-80CE-81AE4E8B82E3}.Release|Any CPU.Build.0 = Release|Any CPU
34+
{03A5C03C-2BF9-4EF5-9473-867F2D31BFC6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
35+
{03A5C03C-2BF9-4EF5-9473-867F2D31BFC6}.Debug|Any CPU.Build.0 = Debug|Any CPU
36+
{03A5C03C-2BF9-4EF5-9473-867F2D31BFC6}.Release|Any CPU.ActiveCfg = Release|Any CPU
37+
{03A5C03C-2BF9-4EF5-9473-867F2D31BFC6}.Release|Any CPU.Build.0 = Release|Any CPU
3238
EndGlobalSection
3339
GlobalSection(SolutionProperties) = preSolution
3440
HideSolutionNode = FALSE
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Our.Umbraco.DataAnnotations.Interfaces
8+
{
9+
public interface IUmbracoValidationAttribute
10+
{
11+
string DictionaryKey { get; set; }
12+
}
13+
}

src/Our.Umbraco.DataAnnotations/Our.Umbraco.DataAnnotations.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@
221221
<ItemGroup>
222222
<Compile Include="Conditionals\ConditionalValidationAttributes.cs" />
223223
<Compile Include="Constants.cs" />
224+
<Compile Include="Interfaces\IUmbracoValidationAttribute.cs" />
224225
<Compile Include="Migrations\CreateDictionaryKeys.cs" />
225226
<Compile Include="Migrations\Runner.cs" />
226227
<Compile Include="Properties\AssemblyInfo.cs" />

src/Our.Umbraco.DataAnnotations/UmbracoCompareAttribute.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
using System.Collections.Generic;
1+
using Our.Umbraco.DataAnnotations.Interfaces;
2+
using System.Collections.Generic;
23
using System.Web.Mvc;
34

45
namespace Our.Umbraco.DataAnnotations
56
{
67
/// <summary>
78
/// Specified that two properties data field value must match.
89
/// </summary>
9-
public class UmbracoCompareAttribute : System.ComponentModel.DataAnnotations.CompareAttribute, IClientValidatable
10+
public sealed class UmbracoCompareAttribute : System.ComponentModel.DataAnnotations.CompareAttribute, IClientValidatable, IUmbracoValidationAttribute
1011
{
12+
public string DictionaryKey { get; set; } = "EqualToError";
1113
public new string ErrorMessageString { get; set; }
1214
public new string OtherPropertyDisplayName { get; set; }
1315

1416
public UmbracoCompareAttribute(string otherProperty)
1517
: base(otherProperty)
1618
{
17-
ErrorMessageString = UmbracoDictionary.GetDictionaryValue("EqualToError");
19+
ErrorMessageString = UmbracoDictionary.GetDictionaryValue(DictionaryKey);
1820
}
1921

2022
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)

src/Our.Umbraco.DataAnnotations/UmbracoDictionary.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Our.Umbraco.DataAnnotations
44
{
5-
public class UmbracoDictionary
5+
public sealed class UmbracoDictionary
66
{
77
private static UmbracoHelper _helper;
88

src/Our.Umbraco.DataAnnotations/UmbracoDisplayNameAttribute.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
using System.ComponentModel;
1+
using Our.Umbraco.DataAnnotations.Interfaces;
2+
using System.ComponentModel;
23

34
namespace Our.Umbraco.DataAnnotations
45
{
5-
public class UmbracoDisplayNameAttribute : DisplayNameAttribute
6+
public sealed class UmbracoDisplayNameAttribute : DisplayNameAttribute, IUmbracoValidationAttribute
67
{
7-
private readonly string dictionaryKey;
8+
public string DictionaryKey { get; set; }
89

910
public UmbracoDisplayNameAttribute(string dictionaryKey)
1011
: base()
1112
{
12-
this.dictionaryKey = dictionaryKey;
13+
DictionaryKey = dictionaryKey;
1314
}
1415

1516
public override string DisplayName
1617
{
1718
get
1819
{
19-
return UmbracoDictionary.GetDictionaryValue(dictionaryKey);
20+
return UmbracoDictionary.GetDictionaryValue(DictionaryKey);
2021
}
2122
}
2223
}

src/Our.Umbraco.DataAnnotations/UmbracoEmailAddressAttribute.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using Our.Umbraco.DataAnnotations.Interfaces;
2+
using System.Collections.Generic;
23
using System.ComponentModel.DataAnnotations;
34
using System.Web.Mvc;
45

@@ -7,7 +8,7 @@ namespace Our.Umbraco.DataAnnotations
78
/// <summary>
89
/// Specifies that a data field value must be a valid Email Address
910
/// </summary>
10-
public class UmbracoEmailAddressAttribute : RegularExpressionAttribute, IClientValidatable
11+
public sealed class UmbracoEmailAddressAttribute : RegularExpressionAttribute, IClientValidatable, IUmbracoValidationAttribute
1112
{
1213
public string DictionaryKey { get; set; } = "EmailError";
1314

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using Our.Umbraco.DataAnnotations.Interfaces;
2+
using System.Collections.Generic;
23
using System.ComponentModel.DataAnnotations;
34
using System.Web.Mvc;
45

@@ -7,24 +8,20 @@ namespace Our.Umbraco.DataAnnotations
78
/// <summary>
89
/// Specifies the maximum length of array or string data allowed in a property.
910
/// </summary>
10-
public class UmbracoMaxLengthAttribute : MaxLengthAttribute, IClientValidatable
11+
public sealed class UmbracoMaxLengthAttribute : MaxLengthAttribute, IClientValidatable, IUmbracoValidationAttribute
1112
{
13+
public string DictionaryKey { get; set; } = "MaxLengthError";
14+
1215
public UmbracoMaxLengthAttribute(int length)
1316
: base(length)
1417
{
18+
ErrorMessage = UmbracoDictionary.GetDictionaryValue(DictionaryKey);
1519
}
1620

1721
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
1822
{
19-
ErrorMessage = UmbracoDictionary.GetDictionaryValue("MaxLengthError");
2023
yield return
2124
new ModelClientValidationMaxLengthRule(FormatErrorMessage(metadata.GetDisplayName()), Length);
2225
}
23-
24-
public UmbracoMaxLengthAttribute(int length, string dictionaryKey)
25-
: base(length)
26-
{
27-
ErrorMessage = UmbracoDictionary.GetDictionaryValue(dictionaryKey);
28-
}
2926
}
3027
}
Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using Our.Umbraco.DataAnnotations.Interfaces;
2+
using System.Collections.Generic;
23
using System.ComponentModel.DataAnnotations;
34
using System.Web.Mvc;
45

@@ -7,24 +8,20 @@ namespace Our.Umbraco.DataAnnotations
78
/// <summary>
89
/// Specifies the minimum length of array or string data allowed in a property.
910
/// </summary>
10-
public class UmbracoMinLengthAttribute : MinLengthAttribute, IClientValidatable
11+
public sealed class UmbracoMinLengthAttribute : MinLengthAttribute, IClientValidatable, IUmbracoValidationAttribute
1112
{
13+
public string DictionaryKey { get; set; } = "MinLengthError";
14+
1215
public UmbracoMinLengthAttribute(int length)
1316
: base(length)
1417
{
18+
ErrorMessage = UmbracoDictionary.GetDictionaryValue(DictionaryKey);
1519
}
1620

1721
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
1822
{
19-
ErrorMessage = UmbracoDictionary.GetDictionaryValue("MinLengthError");
2023
yield return
2124
new ModelClientValidationMinLengthRule(FormatErrorMessage(metadata.GetDisplayName()), Length);
2225
}
23-
24-
public UmbracoMinLengthAttribute(int length, string dictionaryKey)
25-
: base(length)
26-
{
27-
ErrorMessage = UmbracoDictionary.GetDictionaryValue(dictionaryKey);
28-
}
2926
}
3027
}

src/Our.Umbraco.DataAnnotations/UmbracoMustBeTrueAttribute.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
using System;
1+
using Our.Umbraco.DataAnnotations.Interfaces;
2+
using System;
23
using System.Collections.Generic;
34
using System.ComponentModel.DataAnnotations;
45
using System.Web.Mvc;
56

67
namespace Our.Umbraco.DataAnnotations
78
{
89
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
9-
public class UmbracoMustBeTrueAttribute : ValidationAttribute, IClientValidatable
10+
public sealed class UmbracoMustBeTrueAttribute : ValidationAttribute, IClientValidatable, IUmbracoValidationAttribute
1011
{
12+
public string DictionaryKey { get; set; } = "MustBeTrueError";
13+
1114
public UmbracoMustBeTrueAttribute()
1215
{
13-
ErrorMessage = UmbracoDictionary.GetDictionaryValue("MustBeTrueError");
16+
ErrorMessage = UmbracoDictionary.GetDictionaryValue(DictionaryKey);
1417
}
1518

1619
public override bool IsValid(object value)

src/Our.Umbraco.DataAnnotations/UmbracoPasswordAttribute.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Our.Umbraco.DataAnnotations.Interfaces;
2+
using System;
23
using System.Collections.Generic;
34
using System.ComponentModel.DataAnnotations;
45
using System.Globalization;
@@ -13,18 +14,22 @@ namespace Our.Umbraco.DataAnnotations
1314
/// Validates the string data using default membership provider.
1415
/// </summary>
1516
/// https://referencesource.microsoft.com/#System.Web/Security/MembershipPasswordAttribute.cs,19c2a804c4a5eaf5,references
16-
public class UmbracoPasswordAttribute : MembershipPasswordAttribute, IClientValidatable
17+
public sealed class UmbracoPasswordAttribute : MembershipPasswordAttribute, IClientValidatable, IUmbracoValidationAttribute
1718
{
19+
public string DictionaryKey { get; set; } = "PasswordError";
20+
public string MinPasswordLengthDictionaryKey { get; set; } = "MinPasswordLengthError";
21+
public string MinNonAlphanumericCharactersDictionaryKey { get; set; } = "MinPasswordLengthError";
22+
public string PasswordStrengthDictionaryKey { get; set; } = "MinPasswordLengthError";
1823
public int? PasswordStrengthRegexTimeout { get; set; }
1924
public string ValidationName = "password";
2025

2126
public UmbracoPasswordAttribute()
2227
: base()
2328
{
24-
ErrorMessage = UmbracoDictionary.GetDictionaryValue("PasswordError");
25-
MinPasswordLengthError = UmbracoDictionary.GetDictionaryValue("MinPasswordLengthError");
26-
MinNonAlphanumericCharactersError = UmbracoDictionary.GetDictionaryValue("MinNonAlphanumericCharactersError");
27-
PasswordStrengthError = UmbracoDictionary.GetDictionaryValue("PasswordStrengthError");
29+
ErrorMessage = UmbracoDictionary.GetDictionaryValue(DictionaryKey);
30+
MinPasswordLengthError = UmbracoDictionary.GetDictionaryValue(MinPasswordLengthDictionaryKey);
31+
MinNonAlphanumericCharactersError = UmbracoDictionary.GetDictionaryValue(MinNonAlphanumericCharactersDictionaryKey);
32+
PasswordStrengthError = UmbracoDictionary.GetDictionaryValue(PasswordStrengthDictionaryKey);
2833
}
2934

3035
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)

src/Our.Umbraco.DataAnnotations/UmbracoRangeAttribute.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
1-
using System.Collections.Generic;
1+
using Our.Umbraco.DataAnnotations.Interfaces;
2+
using System.Collections.Generic;
23
using System.ComponentModel.DataAnnotations;
34
using System.Web.Mvc;
45

56
namespace Our.Umbraco.DataAnnotations
67
{
7-
public class UmbracoRangeAttribute : RangeAttribute, IClientValidatable
8+
public sealed class UmbracoRangeAttribute : RangeAttribute, IClientValidatable, IUmbracoValidationAttribute
89
{
9-
public string ResourceKey { get; set; } = "RangeError";
10+
public string DictionaryKey { get; set; } = "RangeError";
1011

1112
public UmbracoRangeAttribute(int minimum, int maximum)
1213
: base(minimum, maximum)
1314
{
15+
ErrorMessage = UmbracoDictionary.GetDictionaryValue(DictionaryKey);
1416
}
1517

1618
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
1719
{
18-
ErrorMessage = UmbracoDictionary.GetDictionaryValue(ResourceKey);
1920
yield return
2021
new ModelClientValidationRangeRule(FormatErrorMessage(metadata.GetDisplayName()), Minimum, Maximum);
2122
}

0 commit comments

Comments
 (0)