diff --git a/DataAnnotationsExtensions.Core/DataAnnotationsExtensions.Core.csproj b/DataAnnotationsExtensions.Core/DataAnnotationsExtensions.Core.csproj index 434846d..ecfce99 100644 --- a/DataAnnotationsExtensions.Core/DataAnnotationsExtensions.Core.csproj +++ b/DataAnnotationsExtensions.Core/DataAnnotationsExtensions.Core.csproj @@ -1,76 +1,77 @@ - - - - Debug - AnyCPU - 8.0.30703 - 2.0 - {144683DA-A20C-4A69-9310-1E21FE38B67B} - Library - Properties - DataAnnotationsExtensions.Core - DataAnnotationsExtensions.Core - v4.0 - 512 - - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - - - - - - - - - - - Properties\CommonAssemblyInfo.cs - - - - - - - - - - - - - - - - - - - {FE938484-722F-40F6-AC72-3B8C8533CA54} - DataAnnotationsExtensions - - - - + + + + Debug + AnyCPU + 8.0.30703 + 2.0 + {144683DA-A20C-4A69-9310-1E21FE38B67B} + Library + Properties + DataAnnotationsExtensions.Core + DataAnnotationsExtensions.Core + v4.0 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + Properties\CommonAssemblyInfo.cs + + + + + + + + + + + + + + + + + + + + {FE938484-722F-40F6-AC72-3B8C8533CA54} + DataAnnotationsExtensions + + + + \ No newline at end of file diff --git a/DataAnnotationsExtensions.Core/NotNullOrZeroEntity.cs b/DataAnnotationsExtensions.Core/NotNullOrZeroEntity.cs new file mode 100644 index 0000000..bf9518f --- /dev/null +++ b/DataAnnotationsExtensions.Core/NotNullOrZeroEntity.cs @@ -0,0 +1,11 @@ +namespace DataAnnotationsExtensions.Core +{ + public class NotNullOrZeroEntity + { + [NotNullOrZero] + public int? NotNullOrZeroInt { get; set; } + + [NotNullOrZero] + public double? NotNullOrZeroDouble { get; set; } + } +} diff --git a/DataAnnotationsExtensions.Tests/DataAnnotationsExtensions.Tests.csproj b/DataAnnotationsExtensions.Tests/DataAnnotationsExtensions.Tests.csproj index 53eb3e2..0c76b27 100644 --- a/DataAnnotationsExtensions.Tests/DataAnnotationsExtensions.Tests.csproj +++ b/DataAnnotationsExtensions.Tests/DataAnnotationsExtensions.Tests.csproj @@ -66,6 +66,7 @@ + diff --git a/DataAnnotationsExtensions.Tests/ValidationAttributes/NotNullOrZeroAttributeTests.cs b/DataAnnotationsExtensions.Tests/ValidationAttributes/NotNullOrZeroAttributeTests.cs new file mode 100644 index 0000000..f60f2b7 --- /dev/null +++ b/DataAnnotationsExtensions.Tests/ValidationAttributes/NotNullOrZeroAttributeTests.cs @@ -0,0 +1,89 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Globalization; +using System.Linq; +using System.Text; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace DataAnnotationsExtensions.Tests.ValidationAttributes +{ + [TestClass] + public class NotNullOrZeroAttributeTests : TestBase + { + [TestMethod] + public void IsValidIntegerTests() + { + var attribute = new NotNullOrZeroAttribute(); + + Assert.IsFalse(attribute.IsValid(null)); // Null values are invalid + Assert.IsTrue(attribute.IsValid("42")); + Assert.IsTrue(attribute.IsValid(42.5)); + Assert.IsTrue(attribute.IsValid("50")); + Assert.IsTrue(attribute.IsValid(100)); + Assert.IsTrue(attribute.IsValid("10000000000")); + Assert.IsFalse(attribute.IsValid(0)); + Assert.IsFalse(attribute.IsValid("0")); + Assert.IsTrue(attribute.IsValid(-50)); + Assert.IsFalse(attribute.IsValid("fifty")); + } + + [TestMethod] + public void IsValidDoubleTests() + { + var attribute = new NotNullOrZeroAttribute(); + + Assert.IsFalse(attribute.IsValid(null)); // Null values are invalid + Assert.IsTrue(attribute.IsValid("4")); + Assert.IsTrue(attribute.IsValid("4.5")); + Assert.IsTrue(attribute.IsValid(100)); + Assert.IsTrue(attribute.IsValid("100.42")); + Assert.IsFalse(attribute.IsValid(0)); + Assert.IsFalse(attribute.IsValid("0")); + Assert.IsFalse(attribute.IsValid("INVALID STRING")); + } + + [TestMethod] + public void ErrorResourcesTest() + { + var attribute = new NotNullOrZeroAttribute(); + attribute.ErrorMessageResourceName = "ErrorMessage"; + attribute.ErrorMessageResourceType = typeof(ErrorResources); + + const string invalidValue = "a"; + + var result = attribute.GetValidationResult(invalidValue, new ValidationContext(0, null, null)); + + Assert.AreEqual("error message", result.ErrorMessage); + } + + [TestMethod] + public void GlobalizedErrorResourcesTest() + { + System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("es-MX"); + + var attribute = new NotNullOrZeroAttribute(); + attribute.ErrorMessageResourceName = "ErrorMessage"; + attribute.ErrorMessageResourceType = typeof(ErrorResources); + + const string invalidValue = "a"; + + var result = attribute.GetValidationResult(invalidValue, new ValidationContext(0, null, null)); + + Assert.AreEqual("mensaje de error", result.ErrorMessage); + } + + [TestMethod] + public void ErrorMessageTest() + { + var attribute = new NotNullOrZeroAttribute(); + attribute.ErrorMessage = "SampleErrorMessage"; + + const string invalidValue = "a"; + + var result = attribute.GetValidationResult(invalidValue, new ValidationContext(0, null, null)); + + Assert.AreEqual("SampleErrorMessage", result.ErrorMessage); + } + } +} diff --git a/DataAnnotationsExtensions.Web/Controllers/NotNullOrZeroController.cs b/DataAnnotationsExtensions.Web/Controllers/NotNullOrZeroController.cs new file mode 100644 index 0000000..adfcbbe --- /dev/null +++ b/DataAnnotationsExtensions.Web/Controllers/NotNullOrZeroController.cs @@ -0,0 +1,8 @@ +using DataAnnotationsExtensions.Core; + +namespace DataAnnotationsExtensions.Web.Controllers +{ + public class NotNullOrZeroController : ValidationControllerBase + { + } +} diff --git a/DataAnnotationsExtensions.Web/DataAnnotationsExtensions.Web.csproj b/DataAnnotationsExtensions.Web/DataAnnotationsExtensions.Web.csproj index 1389ea4..e83148b 100644 --- a/DataAnnotationsExtensions.Web/DataAnnotationsExtensions.Web.csproj +++ b/DataAnnotationsExtensions.Web/DataAnnotationsExtensions.Web.csproj @@ -1,235 +1,237 @@ - - - - Debug - AnyCPU - - - 2.0 - {16249BAA-A6E2-4D9E-83E1-FF83C8564A83} - {E53F8FEA-EAE0-44A6-8774-FFD645390401};{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc} - Library - Properties - DataAnnotationsExtensions.Web - DataAnnotationsExtensions.Web - v4.0 - false - false - - - true - full - false - bin\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - bin\ - TRACE - prompt - 4 - - - - - - - - - - - - - - - - - - - - - - - - - - - - Properties\CommonAssemblyInfo.cs - - - - - - - - - - - - - - - - - - Global.asax - - - - - - - - - - - - - - - - - - - - - - - - - Web.config - - - Web.config - - - - - - - - - - - - - - - - - - - - - Designer - - - - - - - - - - - - {CE731EE1-C6D0-4183-803D-14729BBE2895} - DataAnnotationsExtensions.ClientValidation - - - {144683DA-A20C-4A69-9310-1E21FE38B67B} - DataAnnotationsExtensions.Core - - - {FE938484-722F-40F6-AC72-3B8C8533CA54} - DataAnnotationsExtensions - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - False - True - 34932 - / - - - False - False - - - False - - - - + + + + Debug + AnyCPU + + + 2.0 + {16249BAA-A6E2-4D9E-83E1-FF83C8564A83} + {E53F8FEA-EAE0-44A6-8774-FFD645390401};{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc} + Library + Properties + DataAnnotationsExtensions.Web + DataAnnotationsExtensions.Web + v4.0 + false + false + + + true + full + false + bin\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + + + + + + + Properties\CommonAssemblyInfo.cs + + + + + + + + + + + + + + + + + + + Global.asax + + + + + + + + + + + + + + + + + + + + + + + + + Web.config + + + Web.config + + + + + + + + + + + + + + + + + + + + + Designer + + + + + + + + + + + + {CE731EE1-C6D0-4183-803D-14729BBE2895} + DataAnnotationsExtensions.ClientValidation + + + {144683DA-A20C-4A69-9310-1E21FE38B67B} + DataAnnotationsExtensions.Core + + + {FE938484-722F-40F6-AC72-3B8C8533CA54} + DataAnnotationsExtensions + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + False + True + 34932 + / + + + False + False + + + False + + + + \ No newline at end of file diff --git a/DataAnnotationsExtensions.Web/Views/NotNullOrZero/Create.cshtml b/DataAnnotationsExtensions.Web/Views/NotNullOrZero/Create.cshtml new file mode 100644 index 0000000..d46af14 --- /dev/null +++ b/DataAnnotationsExtensions.Web/Views/NotNullOrZero/Create.cshtml @@ -0,0 +1,25 @@ +@model DataAnnotationsExtensions.Core.NotNullOrZeroEntity + +@{ + ViewBag.Title = "Create"; +} + +

Create

+ +@using (Html.BeginForm()) { + @Html.ValidationSummary(true) +
+ Fields + + @Html.EditorForModel() + +

+ +

+
+} + +
+ @Html.ActionLink("Back to demos", "Demos", "Home") +
+ diff --git a/DataAnnotationsExtensions/DataAnnotationsExtensions.csproj b/DataAnnotationsExtensions/DataAnnotationsExtensions.csproj index 9c66248..b5dbad2 100644 --- a/DataAnnotationsExtensions/DataAnnotationsExtensions.csproj +++ b/DataAnnotationsExtensions/DataAnnotationsExtensions.csproj @@ -12,6 +12,21 @@ DataAnnotationsExtensions v4.0 512 + publish\ + true + Disk + false + Foreground + 7 + Days + false + false + true + 0 + 1.0.0.%2a + false + false + true true @@ -62,6 +77,7 @@ + @@ -81,8 +97,31 @@ + + + + False + Microsoft .NET Framework 4 %28x86 and x64%29 + true + + + False + .NET Framework 3.5 SP1 Client Profile + false + + + False + .NET Framework 3.5 SP1 + false + + + False + Windows Installer 3.1 + true + +