Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ionsExtensions into cuit-rodrigojuarez
  • Loading branch information
srkirkland committed Jun 1, 2011
2 parents e106a62 + fcb58c1 commit 2c6945b
Show file tree
Hide file tree
Showing 14 changed files with 266 additions and 19 deletions.
@@ -0,0 +1,19 @@
using System.Collections.Generic;
using System.Web.Mvc;
using DataAnnotationsExtensions.ClientValidation.Rules;

namespace DataAnnotationsExtensions.ClientValidation.Adapters
{
public class CuitAttributeAdapter : DataAnnotationsModelValidator<CuitAttribute>
{
public CuitAttributeAdapter(ModelMetadata metadata, ControllerContext context, CuitAttribute attribute)
: base(metadata, context, attribute)
{
}

public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
{
return new[] { new ModelClientValidationCuitRule(ErrorMessage) };
}
}
}
Expand Up @@ -46,6 +46,7 @@
<Link>Properties\CommonAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Adapters\CreditCardAttributeAdapter.cs" />
<Compile Include="Adapters\CuitAttributeAdapter.cs" />
<Compile Include="Adapters\DateAttributeAdapter.cs" />
<Compile Include="Adapters\DigitsAttributeAdapter.cs" />
<Compile Include="Adapters\EmailAttributeAdapter.cs" />
Expand All @@ -62,6 +63,7 @@
<DependentUpon>ClientValidationResources.resx</DependentUpon>
</Compile>
<Compile Include="Rules\ModelClientValidationCreditCardRule.cs" />
<Compile Include="Rules\ModelClientValidationCuitRule.cs" />
<Compile Include="Rules\ModelClientValidationDateRule.cs" />
<Compile Include="Rules\ModelClientValidationDigitsRule.cs" />
<Compile Include="Rules\ModelClientValidationEmailRule.cs" />
Expand Down
@@ -0,0 +1,13 @@
using System.Web.Mvc;

namespace DataAnnotationsExtensions.ClientValidation.Rules
{
public class ModelClientValidationCuitRule : ModelClientValidationRule
{
public ModelClientValidationCuitRule(string errorMessage)
{
ErrorMessage = errorMessage;
ValidationType = "cuit";
}
}
}
11 changes: 11 additions & 0 deletions 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; }
}
}
Expand Up @@ -45,6 +45,7 @@
<Link>Properties\CommonAssemblyInfo.cs</Link>
</Compile>
<Compile Include="CreditCardEntity.cs" />
<Compile Include="CuitEntity.cs" />
<Compile Include="DateEntity.cs" />
<Compile Include="DigitsEntity.cs" />
<Compile Include="EmailEntity.cs" />
Expand Down
Expand Up @@ -60,6 +60,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Controllers\HomeControllerTest.cs" />
<Compile Include="ValidationAttributes\CreditCardAttributeTests.cs" />
<Compile Include="ValidationAttributes\CuitAttributeTests.cs" />
<Compile Include="ValidationAttributes\DateAttributeTests.cs" />
<Compile Include="ValidationAttributes\DigitsAttributeTests.cs" />
<Compile Include="ValidationAttributes\EmailAttributeTests.cs" />
Expand Down
@@ -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);
}
}
}
13 changes: 13 additions & 0 deletions DataAnnotationsExtensions.Web/Controllers/CuitController.cs
@@ -0,0 +1,13 @@
using DataAnnotationsExtensions.Core;

namespace DataAnnotationsExtensions.Web.Controllers
{
public class CuitController : ValidationControllerBase<CuitEntity>
{
protected override void AddMessage()
{
Message =
"CUIT validation: Examples of valid values {20245597151, 20-24559715-1}.";
}
}
}
Expand Up @@ -14,6 +14,7 @@
<AssemblyName>DataAnnotationsExtensions.Web</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<MvcBuildViews>false</MvcBuildViews>
<UseIISExpress>false</UseIISExpress>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -61,6 +62,7 @@
<Link>Properties\CommonAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Controllers\CreditCardController.cs" />
<Compile Include="Controllers\CuitController.cs" />
<Compile Include="Controllers\DateController.cs" />
<Compile Include="Controllers\DigitsController.cs" />
<Compile Include="Controllers\EmailController.cs" />
Expand Down Expand Up @@ -196,6 +198,7 @@
<Content Include="Views\Home\Wiki.cshtml" />
<Content Include="Views\Shared\_Analytics.cshtml" />
<Content Include="Views\Home\_Packages.cshtml" />
<Content Include="Views\Cuit\Create.cshtml" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
Expand Down
25 changes: 25 additions & 0 deletions DataAnnotationsExtensions.Web/Views/Cuit/Create.cshtml
@@ -0,0 +1,25 @@
@model DataAnnotationsExtensions.Core.CuitEntity

@{
ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Fields</legend>

@Html.EditorForModel()

<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}

<div>
@Html.ActionLink("Back to demos", "Demos", "Home")
</div>

90 changes: 90 additions & 0 deletions 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<int> prefijosValidos = new List<int>(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;
}
}
}
}
1 change: 1 addition & 0 deletions DataAnnotationsExtensions/DataAnnotationsExtensions.csproj
Expand Up @@ -45,6 +45,7 @@
<Link>Properties\CommonAssemblyInfo.cs</Link>
</Compile>
<Compile Include="CreditCardAttribute.cs" />
<Compile Include="CuitAttribute.cs" />
<Compile Include="DateAttribute.cs" />
<Compile Include="DigitsAttribute.cs" />
<Compile Include="EmailAttribute.cs" />
Expand Down

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

39 changes: 21 additions & 18 deletions DataAnnotationsExtensions/Resources/ValidatorResources.resx
Expand Up @@ -264,22 +264,25 @@
<data name="EqualTo_UnknownProperty" xml:space="preserve">
<value>Could not find a property named {0}.</value>
</data>
<data name="NumericAttribute_Invalid">
<value xml:space="preserve">The {0} field is not a valid number.</value>
</data>
<data name="DigitsAttribute_Invalid">
<value xml:space="preserve">The field {0} should contain only digits</value>
</data>
<data name="MaxAttribute_Invalid">
<value xml:space="preserve">The field {0} must be less than or equal to {1}</value>
</data>
<data name="MinAttribute_Invalid">
<value xml:space="preserve">The field {0} must be greater than or equal to {1}</value>
</data>
<data name="DateAttribute_Invalid">
<value xml:space="preserve">The field {0} is not a valid date</value>
</data>
<data name="IntegerAttribute_Invalid">
<value xml:space="preserve">The field {0} should be a positive or negative non-decimal number.</value>
</data>
<data name="NumericAttribute_Invalid" xml:space="preserve">
<value>The {0} field is not a valid number.</value>
</data>
<data name="DigitsAttribute_Invalid" xml:space="preserve">
<value>The field {0} should contain only digits</value>
</data>
<data name="MaxAttribute_Invalid" xml:space="preserve">
<value>The field {0} must be less than or equal to {1}</value>
</data>
<data name="MinAttribute_Invalid" xml:space="preserve">
<value>The field {0} must be greater than or equal to {1}</value>
</data>
<data name="DateAttribute_Invalid" xml:space="preserve">
<value>The field {0} is not a valid date</value>
</data>
<data name="IntegerAttribute_Invalid" xml:space="preserve">
<value>The field {0} should be a positive or negative non-decimal number.</value>
</data>
<data name="CuitAttribute_Invalid" xml:space="preserve">
<value>The {0} field is not a valid CUIT number.</value>
</data>
</root>

0 comments on commit 2c6945b

Please sign in to comment.