diff --git a/DataAnnotationsExtensions.ClientValidation/Adapters/CuitAttributeAdapter.cs b/DataAnnotationsExtensions.ClientValidation/Adapters/CuitAttributeAdapter.cs new file mode 100644 index 0000000..8545f84 --- /dev/null +++ b/DataAnnotationsExtensions.ClientValidation/Adapters/CuitAttributeAdapter.cs @@ -0,0 +1,19 @@ +using System.Collections.Generic; +using System.Web.Mvc; +using DataAnnotationsExtensions.ClientValidation.Rules; + +namespace DataAnnotationsExtensions.ClientValidation.Adapters +{ + public class CuitAttributeAdapter : DataAnnotationsModelValidator + { + public CuitAttributeAdapter(ModelMetadata metadata, ControllerContext context, CuitAttribute attribute) + : base(metadata, context, attribute) + { + } + + public override IEnumerable GetClientValidationRules() + { + return new[] { new ModelClientValidationCuitRule(ErrorMessage) }; + } + } +} \ No newline at end of file diff --git a/DataAnnotationsExtensions.ClientValidation/DataAnnotationsExtensions.ClientValidation.csproj b/DataAnnotationsExtensions.ClientValidation/DataAnnotationsExtensions.ClientValidation.csproj index 7b85c9a..ff17b8b 100644 --- a/DataAnnotationsExtensions.ClientValidation/DataAnnotationsExtensions.ClientValidation.csproj +++ b/DataAnnotationsExtensions.ClientValidation/DataAnnotationsExtensions.ClientValidation.csproj @@ -46,6 +46,7 @@ Properties\CommonAssemblyInfo.cs + @@ -62,6 +63,7 @@ ClientValidationResources.resx + diff --git a/DataAnnotationsExtensions.ClientValidation/Rules/ModelClientValidationCuitRule.cs b/DataAnnotationsExtensions.ClientValidation/Rules/ModelClientValidationCuitRule.cs new file mode 100644 index 0000000..3f28d77 --- /dev/null +++ b/DataAnnotationsExtensions.ClientValidation/Rules/ModelClientValidationCuitRule.cs @@ -0,0 +1,13 @@ +using System.Web.Mvc; + +namespace DataAnnotationsExtensions.ClientValidation.Rules +{ + public class ModelClientValidationCuitRule : ModelClientValidationRule + { + public ModelClientValidationCuitRule(string errorMessage) + { + ErrorMessage = errorMessage; + ValidationType = "cuit"; + } + } +} \ No newline at end of file diff --git a/DataAnnotationsExtensions.Core/CuitEntity.cs b/DataAnnotationsExtensions.Core/CuitEntity.cs new file mode 100644 index 0000000..a91a492 --- /dev/null +++ b/DataAnnotationsExtensions.Core/CuitEntity.cs @@ -0,0 +1,11 @@ +using System.ComponentModel.DataAnnotations; + +namespace DataAnnotationsExtensions.Core +{ + public class CuitEntity + { + [Cuit] + [Required] + public string Cuit { get; set; } + } +} \ No newline at end of file diff --git a/DataAnnotationsExtensions.Core/DataAnnotationsExtensions.Core.csproj b/DataAnnotationsExtensions.Core/DataAnnotationsExtensions.Core.csproj index 8ad6c4f..f80570f 100644 --- a/DataAnnotationsExtensions.Core/DataAnnotationsExtensions.Core.csproj +++ b/DataAnnotationsExtensions.Core/DataAnnotationsExtensions.Core.csproj @@ -45,6 +45,7 @@ Properties\CommonAssemblyInfo.cs + diff --git a/DataAnnotationsExtensions.Tests/DataAnnotationsExtensions.Tests.csproj b/DataAnnotationsExtensions.Tests/DataAnnotationsExtensions.Tests.csproj index d797361..74fd3f3 100644 --- a/DataAnnotationsExtensions.Tests/DataAnnotationsExtensions.Tests.csproj +++ b/DataAnnotationsExtensions.Tests/DataAnnotationsExtensions.Tests.csproj @@ -60,6 +60,7 @@ + diff --git a/DataAnnotationsExtensions.Tests/ValidationAttributes/CuitAttributeTests.cs b/DataAnnotationsExtensions.Tests/ValidationAttributes/CuitAttributeTests.cs new file mode 100644 index 0000000..cfacbeb --- /dev/null +++ b/DataAnnotationsExtensions.Tests/ValidationAttributes/CuitAttributeTests.cs @@ -0,0 +1,56 @@ +using System.ComponentModel.DataAnnotations; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace DataAnnotationsExtensions.Tests.ValidationAttributes +{ + [TestClass] + public class CuitAttributeTests + { + [TestMethod] + public void IsValidTests() + { + var attribute = new CuitAttribute(); + + Assert.IsTrue(attribute.IsValid(null)); + Assert.IsTrue(attribute.IsValid("20245597151")); + Assert.IsTrue(attribute.IsValid("20-24559715-1")); + Assert.IsTrue(attribute.IsValid("27-23840320-6")); + Assert.IsTrue(attribute.IsValid("27238403206")); + + Assert.IsFalse(attribute.IsValid("20 24559715 1")); + Assert.IsFalse(attribute.IsValid("")); + Assert.IsFalse(attribute.IsValid("99-99999999-9")); + Assert.IsFalse(attribute.IsValid("99999999999")); + Assert.IsFalse(attribute.IsValid("4408 0412 3456 7890")); + Assert.IsFalse(attribute.IsValid(0)); + Assert.IsFalse(attribute.IsValid(20245597151)); + } + + [TestMethod] + public void ErrorResourcesTest() + { + var attribute = new CuitAttribute(); + attribute.ErrorMessageResourceName = "ErrorMessage"; + attribute.ErrorMessageResourceType = typeof (ErrorResources); + + const int invalidValue = 0; + + var result = attribute.GetValidationResult(invalidValue, new ValidationContext(0, null, null)); + + Assert.AreEqual(ErrorResources.ErrorMessage, result.ErrorMessage); + } + + [TestMethod] + public void ErrorMessageTest() + { + var attribute = new CuitAttribute(); + attribute.ErrorMessage = "SampleErrorMessage"; + + const int invalidValue = 0; + + var result = attribute.GetValidationResult(invalidValue, new ValidationContext(0, null, null)); + + Assert.AreEqual("SampleErrorMessage", result.ErrorMessage); + } + } +} \ No newline at end of file diff --git a/DataAnnotationsExtensions.Web/Controllers/CuitController.cs b/DataAnnotationsExtensions.Web/Controllers/CuitController.cs new file mode 100644 index 0000000..bff8369 --- /dev/null +++ b/DataAnnotationsExtensions.Web/Controllers/CuitController.cs @@ -0,0 +1,13 @@ +using DataAnnotationsExtensions.Core; + +namespace DataAnnotationsExtensions.Web.Controllers +{ + public class CuitController : ValidationControllerBase + { + protected override void AddMessage() + { + Message = + "CUIT validation: Examples of valid values {20245597151, 20-24559715-1}."; + } + } +} \ No newline at end of file diff --git a/DataAnnotationsExtensions.Web/DataAnnotationsExtensions.Web.csproj b/DataAnnotationsExtensions.Web/DataAnnotationsExtensions.Web.csproj index 33c4863..e26ceda 100644 --- a/DataAnnotationsExtensions.Web/DataAnnotationsExtensions.Web.csproj +++ b/DataAnnotationsExtensions.Web/DataAnnotationsExtensions.Web.csproj @@ -14,6 +14,7 @@ DataAnnotationsExtensions.Web v4.0 false + false true @@ -61,6 +62,7 @@ Properties\CommonAssemblyInfo.cs + @@ -196,6 +198,7 @@ + diff --git a/DataAnnotationsExtensions.Web/Views/Cuit/Create.cshtml b/DataAnnotationsExtensions.Web/Views/Cuit/Create.cshtml new file mode 100644 index 0000000..3e35f6f --- /dev/null +++ b/DataAnnotationsExtensions.Web/Views/Cuit/Create.cshtml @@ -0,0 +1,25 @@ +@model DataAnnotationsExtensions.Core.CuitEntity + +@{ + ViewBag.Title = "Create"; +} + +

Create

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

+ +

+
+} + +
+ @Html.ActionLink("Back to demos", "Demos", "Home") +
+ diff --git a/DataAnnotationsExtensions/CuitAttribute.cs b/DataAnnotationsExtensions/CuitAttribute.cs new file mode 100644 index 0000000..0e6a061 --- /dev/null +++ b/DataAnnotationsExtensions/CuitAttribute.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using DataAnnotationsExtensions.Resources; + +namespace DataAnnotationsExtensions +{ + [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] + public class CuitAttribute : DataTypeAttribute + { + public CuitAttribute() + : base("cuit") + { + } + + public override string FormatErrorMessage(string name) + { + if (ErrorMessage == null && ErrorMessageResourceName == null) + { + ErrorMessage = ValidatorResources.CuitAttribute_Invalid; + } + + return base.FormatErrorMessage(name); + } + + public override bool IsValid(object value) + { + if (value == null) + { + return true; + } + + try + { + string cuit = (string)value; + List prefijosValidos = new List(new[] { 20, 22, 23, 27, 30 }); + + int preFijo = 0; + int dni = 0; + int control = 0; + if (cuit.Length == 11 || cuit.Length == 13) + { + if (cuit.Contains("-")) + { + string[] aCuit = cuit.Split('-'); + preFijo = int.Parse(aCuit[0]); + dni = int.Parse(aCuit[1]); + control = int.Parse(aCuit[2]); + } + else + { + preFijo = int.Parse(cuit.Substring(0, 2)); + dni = int.Parse(cuit.Substring(2, 8)); + control = int.Parse(cuit.Substring(10, 1)); + } + } + + string prefijoDni = preFijo + dni.ToString().PadLeft(8, '0'); + int suma = 0; + int n; + + if (!prefijosValidos.Contains(preFijo)) + return false; + + //Algoritmo de calculo del digito verificador + const string coef = "5432765432"; + for (n = 0; n < 10; n++) + suma += int.Parse(prefijoDni.Substring(n, 1)) * int.Parse(coef.Substring(n, 1)); + + int resto = suma % 11; + int digitoVerificador; + + if (resto == 0) + digitoVerificador = 0; + + else if (resto == 10) + digitoVerificador = 1; + + else + digitoVerificador = 11 - resto; + + return (digitoVerificador == control); + } + catch (Exception) + { + return false; + } + } + } +} diff --git a/DataAnnotationsExtensions/DataAnnotationsExtensions.csproj b/DataAnnotationsExtensions/DataAnnotationsExtensions.csproj index 47311cb..d34b2d3 100644 --- a/DataAnnotationsExtensions/DataAnnotationsExtensions.csproj +++ b/DataAnnotationsExtensions/DataAnnotationsExtensions.csproj @@ -45,6 +45,7 @@ Properties\CommonAssemblyInfo.cs
+ diff --git a/DataAnnotationsExtensions/Resources/ValidatorResources.Designer.cs b/DataAnnotationsExtensions/Resources/ValidatorResources.Designer.cs index e162cca..c5f3848 100644 --- a/DataAnnotationsExtensions/Resources/ValidatorResources.Designer.cs +++ b/DataAnnotationsExtensions/Resources/ValidatorResources.Designer.cs @@ -1,7 +1,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:4.0.30319.1 +// Runtime Version:4.0.30319.225 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -150,6 +150,15 @@ internal class ValidatorResources { } } + /// + /// Looks up a localized string similar to The {0} field is not a valid CUIT number.. + /// + internal static string CuitAttribute_Invalid { + get { + return ResourceManager.GetString("CuitAttribute_Invalid", resourceCulture); + } + } + /// /// Looks up a localized string similar to {0} has a DisplayColumn attribute for {1}, but property {1} does not exist.. /// diff --git a/DataAnnotationsExtensions/Resources/ValidatorResources.resx b/DataAnnotationsExtensions/Resources/ValidatorResources.resx index 2a3a528..e0541b4 100644 --- a/DataAnnotationsExtensions/Resources/ValidatorResources.resx +++ b/DataAnnotationsExtensions/Resources/ValidatorResources.resx @@ -264,22 +264,25 @@ Could not find a property named {0}. - - The {0} field is not a valid number. - - - The field {0} should contain only digits - - - The field {0} must be less than or equal to {1} - - - The field {0} must be greater than or equal to {1} - - - The field {0} is not a valid date - - - The field {0} should be a positive or negative non-decimal number. - + + The {0} field is not a valid number. + + + The field {0} should contain only digits + + + The field {0} must be less than or equal to {1} + + + The field {0} must be greater than or equal to {1} + + + The field {0} is not a valid date + + + The field {0} should be a positive or negative non-decimal number. + + + The {0} field is not a valid CUIT number. + \ No newline at end of file