Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error messages from resource #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Intertech.Validation.Test/Intertech.Validation.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<DesignTime>True</DesignTime>
<DependentUpon>TestResource.resx</DependentUpon>
</Compile>
<Compile Include="TestDTO\ValidationTest2.cs" />
<Compile Include="ValidationTestTests.cs" />
<Compile Include="TestDTO\ValidationTest.cs" />
<Compile Include="ValidationHelperTests.cs" />
Expand Down
9 changes: 9 additions & 0 deletions Intertech.Validation.Test/TestDTO/TestResource.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Intertech.Validation.Test/TestDTO/TestResource.resx
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,7 @@
<data name="NameRequiredResource" xml:space="preserve">
<value>Name is required (resource).</value>
</data>
<data name="Required" xml:space="preserve">
<value>Default required {0}</value>
</data>
</root>
63 changes: 63 additions & 0 deletions Intertech.Validation.Test/TestDTO/ValidationTest2.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
22 changes: 22 additions & 0 deletions Intertech.Validation.Test/ValidationHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> { "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]
Expand Down Expand Up @@ -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()
Expand Down
46 changes: 34 additions & 12 deletions Intertech.Validation/Converters/BaseValidationConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -128,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 + "\"");
}
}
Expand All @@ -146,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 + "\"");
}
}
Expand All @@ -164,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 + "\"");
}
}
Expand Down
7 changes: 3 additions & 4 deletions Intertech.Validation/Converters/RangeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 3 additions & 4 deletions Intertech.Validation/Converters/RequiredConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 + "\"");
}
}
Expand Down