From f306d54d0dc06e4fe8f512d0b66b48d3ffec8f7d Mon Sep 17 00:00:00 2001 From: pkelbern Date: Mon, 18 Jul 2016 14:16:32 -0300 Subject: [PATCH 1/2] Default attribute from resource file Get Default attribute messages from the resources --- .../Converters/BaseValidationConverter.cs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Intertech.Validation/Converters/BaseValidationConverter.cs b/Intertech.Validation/Converters/BaseValidationConverter.cs index b9ed315..51da872 100644 --- a/Intertech.Validation/Converters/BaseValidationConverter.cs +++ b/Intertech.Validation/Converters/BaseValidationConverter.cs @@ -115,6 +115,30 @@ protected string GetErrorMessage(string propertyName, CustomAttributeData attr, } } + if(msg == null) + { + //valide the resource default value + try + { + if(attr.AttributeType.Name.EndsWith("Attribute")) + { + var rtype = TypeHelper.GetObjectType(resourceNamespace, true, resourceNamespace, resourceAssemblyName); + if (rtype != null) + { + var resName = rtype.GetProperty(attr.AttributeType.Name.Substring(0, attr.AttributeType.Name.Length - 9 /*Attribute*/), BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); + if (resName != null) + { + msg = resName.GetValue(null) as string; + } + } + } + } + catch + { + msg = null; + } + } + return msg; } From 7e28504fb20c9da9afd107df60d55fd00c666dbe Mon Sep 17 00:00:00 2001 From: pkelbern Date: Mon, 18 Jul 2016 15:53:37 -0300 Subject: [PATCH 2/2] changes --- .../Intertech.Validation.Test.csproj | 1 + .../TestDTO/TestResource.Designer.cs | 9 +++ .../TestDTO/TestResource.resx | 3 + .../TestDTO/ValidationTest2.cs | 63 +++++++++++++++++++ .../ValidationHelperTests.cs | 22 +++++++ .../Converters/BaseValidationConverter.cs | 22 +++---- .../Converters/RangeConverter.cs | 7 +-- .../Converters/RequiredConverter.cs | 7 +-- 8 files changed, 114 insertions(+), 20 deletions(-) create mode 100644 Intertech.Validation.Test/TestDTO/ValidationTest2.cs diff --git a/Intertech.Validation.Test/Intertech.Validation.Test.csproj b/Intertech.Validation.Test/Intertech.Validation.Test.csproj index ced01ed..1557b41 100644 --- a/Intertech.Validation.Test/Intertech.Validation.Test.csproj +++ b/Intertech.Validation.Test/Intertech.Validation.Test.csproj @@ -64,6 +64,7 @@ True TestResource.resx + diff --git a/Intertech.Validation.Test/TestDTO/TestResource.Designer.cs b/Intertech.Validation.Test/TestDTO/TestResource.Designer.cs index ed3a027..33c764d 100644 --- a/Intertech.Validation.Test/TestDTO/TestResource.Designer.cs +++ b/Intertech.Validation.Test/TestDTO/TestResource.Designer.cs @@ -68,5 +68,14 @@ internal static string NameRequiredResource { return ResourceManager.GetString("NameRequiredResource", resourceCulture); } } + + /// + /// Looks up a localized string similar to Default required {0}. + /// + internal static string Required { + get { + return ResourceManager.GetString("Required", resourceCulture); + } + } } } diff --git a/Intertech.Validation.Test/TestDTO/TestResource.resx b/Intertech.Validation.Test/TestDTO/TestResource.resx index c67b708..e1f1502 100644 --- a/Intertech.Validation.Test/TestDTO/TestResource.resx +++ b/Intertech.Validation.Test/TestDTO/TestResource.resx @@ -120,4 +120,7 @@ Name is required (resource). + + Default required {0} + \ No newline at end of file diff --git a/Intertech.Validation.Test/TestDTO/ValidationTest2.cs b/Intertech.Validation.Test/TestDTO/ValidationTest2.cs new file mode 100644 index 0000000..df63177 --- /dev/null +++ b/Intertech.Validation.Test/TestDTO/ValidationTest2.cs @@ -0,0 +1,63 @@ +using Intertech.Validation.Constants; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Intertech.Validation.Test.TestDTO +{ + public class ValidationTest2 + { + [Required] + [MinLength(3)] + public string Name { get; set; } + + [CreditCard] + public string CreditCard { get; set; } + + [EmailAddress] + public string Email { get; set; } + + [MaxLength(40)] + public string Street { get; set; } + + [Phone] + public string Phone { get; set; } + + [Range(1, 100)] + public int FavoriteNumber { get; set; } + + [RegularExpression(RegexConstants.Integer)] + public string IntegerString { get; set; } + + [StringLength(30, MinimumLength = 2)] + [Display(Name = "Nick Name")] + public string NickName { get; set; } + + [Url] + public string Website { get; set; } + + [Required] + [MinLength(5, ErrorMessage = ErrorMessages.MinLength)] + [MaxLength(25, ErrorMessage = ErrorMessages.MaxLength)] + public string Length { get; set; } + + [CreditCard(ErrorMessage = ErrorMessages.CreditCard)] + [StringLength(30, MinimumLength = 12, ErrorMessage = ErrorMessages.VisaLength)] + public string Visa { get; set; } + + [Url(ErrorMessage = ErrorMessages.Url)] + public string Url { get; set; } + + [EmailAddress(ErrorMessage = ErrorMessages.Email)] + public string Email2 { get; set; } + + [Phone(ErrorMessage = ErrorMessages.Phone)] + public string Phone2 { get; set; } + + [RegularExpression(RegexConstants.Decimal, ErrorMessage = ErrorMessages.Regex)] + public string DecimalString { get; set; } + } +} diff --git a/Intertech.Validation.Test/ValidationHelperTests.cs b/Intertech.Validation.Test/ValidationHelperTests.cs index c498def..e68eb5e 100644 --- a/Intertech.Validation.Test/ValidationHelperTests.cs +++ b/Intertech.Validation.Test/ValidationHelperTests.cs @@ -76,6 +76,27 @@ public void Init() private void AssertJsonEqual(object expected, object actual) { Assert.IsTrue(JObject.DeepEquals(expected as JObject, actual as JObject)); + } + + [TestMethod] + public void ValidationHelper_Resource_Test() + { + + + var parms = new GetValidationsParms("TestDTO.ValidationTest2", "model") + { + DtoAssemblyNames = new List { "Intertech.Validation.Test" }, + ResourceAssemblyName = "Intertech.Validation.Test", + ResourceNamespace = "Intertech.Validation.Test.TestDTO.TestResource" + }; + + // Act + var valHelper = new ValidationHelper(); + + var ret = valHelper.GetValidations(parms); + + var str = string.Format(TestResource.Required, "Name"); + Assert.IsTrue(ret.ToString().Contains("\"required-msg\": \"" + str + "\"")); } [TestMethod] @@ -134,6 +155,7 @@ public void ValidationHelper_GetValidations_Empty_Test() AssertJsonEqual(_emptyValidations, vals); } + [TestMethod] [ExpectedException(typeof(Exception), "DTO 'blah' not found.")] public void ValidationHelper_GetValidations_DTONotFound_Test() diff --git a/Intertech.Validation/Converters/BaseValidationConverter.cs b/Intertech.Validation/Converters/BaseValidationConverter.cs index 51da872..ab73e73 100644 --- a/Intertech.Validation/Converters/BaseValidationConverter.cs +++ b/Intertech.Validation/Converters/BaseValidationConverter.cs @@ -152,10 +152,9 @@ protected void SetRegularExpressionAAValidation(string propertyName, string disp if (!string.IsNullOrWhiteSpace(displayName)) { var msg = GetErrorMessage(propertyName, attr, resourceNamespace, resourceAssemblyName); - if (string.IsNullOrWhiteSpace(msg)) - { - msg = string.Format(defaultMsgFormat, displayName); - } + + msg = string.Format(string.IsNullOrWhiteSpace(msg) ? defaultMsgFormat : msg, displayName); + jsonString.Append(", 'ng-pattern-msg': \"" + msg + "\""); } } @@ -170,10 +169,10 @@ protected void SetMaxLengthAAValidation(string propertyName, string displayName, if (!string.IsNullOrWhiteSpace(displayName)) { var msg = GetErrorMessage(propertyName, attr, resourceNamespace, resourceAssemblyName); - if (string.IsNullOrWhiteSpace(msg)) - { - msg = string.Format(DataAnnotationConstants.DefaultMaxLengthErrorMsg, displayName, length); - } + + msg = string.Format(string.IsNullOrWhiteSpace(msg) ? DataAnnotationConstants.DefaultMaxLengthErrorMsg : msg, displayName, length); + + jsonString.Append(", 'ng-maxlength-msg': \"" + msg + "\""); } } @@ -188,10 +187,9 @@ protected void SetMinLengthAAValidation(string propertyName, string displayName, if (!string.IsNullOrWhiteSpace(displayName)) { var msg = GetErrorMessage(propertyName, attr, resourceNamespace, resourceAssemblyName); - if (string.IsNullOrWhiteSpace(msg)) - { - msg = string.Format(DataAnnotationConstants.DefaultMinLengthErrorMsg, displayName, length); - } + + msg = string.Format(string.IsNullOrWhiteSpace(msg) ? DataAnnotationConstants.DefaultMinLengthErrorMsg : msg, displayName, length); + jsonString.Append(", 'ng-minlength-msg': \"" + msg + "\""); } } diff --git a/Intertech.Validation/Converters/RangeConverter.cs b/Intertech.Validation/Converters/RangeConverter.cs index 93a641d..e855608 100644 --- a/Intertech.Validation/Converters/RangeConverter.cs +++ b/Intertech.Validation/Converters/RangeConverter.cs @@ -29,10 +29,9 @@ public void Convert(string propertyName, string displayName, CustomAttributeData if (!string.IsNullOrWhiteSpace(displayName)) { var msg = GetErrorMessage(propertyName, attr, resourceNamespace, resourceAssemblyName); - if (string.IsNullOrWhiteSpace(msg)) - { - msg = string.Format(DataAnnotationConstants.DefaultRangeErrorMsg, displayName, minimum, maximum); - } + + msg = string.Format(string.IsNullOrWhiteSpace(msg) ? DataAnnotationConstants.DefaultRangeErrorMsg : msg, displayName, minimum, maximum); + jsonString.Append(", 'min-msg': \"" + msg + "\""); jsonString.Append(", 'max': " + maximum); diff --git a/Intertech.Validation/Converters/RequiredConverter.cs b/Intertech.Validation/Converters/RequiredConverter.cs index b9f4e58..0f69636 100644 --- a/Intertech.Validation/Converters/RequiredConverter.cs +++ b/Intertech.Validation/Converters/RequiredConverter.cs @@ -25,10 +25,9 @@ public void Convert(string propertyName, string displayName, CustomAttributeData if (!string.IsNullOrWhiteSpace(displayName)) { var msg = GetErrorMessage(propertyName, attr, resourceNamespace, resourceAssemblyName); - if (string.IsNullOrWhiteSpace(msg)) - { - msg = string.Format(DataAnnotationConstants.DefaultRequiredErrorMsg, displayName); - } + + msg = string.Format(string.IsNullOrWhiteSpace(msg) ? DataAnnotationConstants.DefaultRequiredErrorMsg : msg, displayName); + jsonString.Append(", 'required-msg': \"" + msg + "\""); } }