quarta-feira, 29 de outubro de 2014

Criando uma Validação Customizada para o CPF – Custom Validation Attribute


1. Apague o arquivo Controller/PessoaController.js e a pasta Views/Pessoa

2. Dentro da pasta “Models” criar a classe “CustomValidationCPFAttribute” com o seguinte conteúdo:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace ProjetoIESB.Models
{
/// <summary>
/// Validação customizada para CPF
/// </summary>
public class CustomValidationCPFAttribute : ValidationAttribute, IClientValidatable
{
/// <summary>
/// Construtor
/// </summary>
public CustomValidationCPFAttribute()
{
}
/// <summary>
/// Validação server
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public override bool IsValid(object value)
{
if (value == null || string.IsNullOrEmpty(value.ToString()))
return true;
bool valido = ValidaCPF(value.ToString());
return valido;
}
/// <summary>
/// Validação client
/// </summary>
/// <param name="metadata"></param>
/// <param name="context"></param>
/// <returns></returns>
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
yield return new ModelClientValidationRule
{
ErrorMessage = this.FormatErrorMessage(null),
ValidationType = "customvalidationcpf"
};
}
// <summary>
/// Remove caracteres não numéricos
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static string RemoveNaoNumericos(string text)
{
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"[^0-9]");
string ret = reg.Replace(text, string.Empty);
return ret;
}
/// <summary>
/// Valida se um cpf é válido
/// </summary>
/// <param name="cpf"></param>
/// <returns></returns>
public static bool ValidaCPF(string cpf)
{
//Remove formatação do número, ex: "123.456.789-01" vira: "12345678901"
cpf = RemoveNaoNumericos(cpf);
if (cpf.Length > 11)
return false;
while (cpf.Length != 11)
cpf = '0' + cpf;
bool igual = true;
for (int i = 1; i < 11 && igual; i++)
if (cpf[i] != cpf[0])
igual = false;
if (igual || cpf == "12345678909")
return false;
int[] numeros = new int[11];
for (int i = 0; i < 11; i++)
numeros[i] = int.Parse(cpf[i].ToString());
int soma = 0;
for (int i = 0; i < 9; i++)
soma += (10 - i) * numeros[i];
int resultado = soma % 11;
if (resultado == 1 || resultado == 0)
{
if (numeros[9] != 0)
return false;
}
else if (numeros[9] != 11 - resultado)
return false;
soma = 0;
for (int i = 0; i < 10; i++)
soma += (11 - i) * numeros[i];
resultado = soma % 11;
if (resultado == 1 || resultado == 0)
{
if (numeros[10] != 0)
return false;
}
else
if (numeros[10] != 11 - resultado)
return false;
return true;
}
}
}


3. Na pasta Models crie ou altere a classe Pessoa deixando o seguinte conteúdo:
using System.ComponentModel.DataAnnotations;
namespace ProjetoIESB.Models
{
public class Pessoa
{
[Key]
public int PessoaID { get; set; }
 
[Required(ErrorMessage = "O Nome é obrigatório.")]
public string Nome { get; set; }
 
public int? Idade { get; set; }
 
[Required]
[DataType(DataType.PhoneNumber)]
[RegularExpression(@"^[2-9]\d{1}-\d{4}-\d{4}$", ErrorMessage = "Você deve digitar um telefone no formato ##-####-####")]
public string Telefone { get; set; }
 
[Required]
[DataType(DataType.PostalCode)]
public string CEP { get; set; }
 
[Required(ErrorMessage = "CPF obrigatório")]
[CustomValidationCPF(ErrorMessage = "CPF inválido")]
public string CPF { get; set; }
}
}

4. No arquivo “IdentityModels.cs”, classe “ApplicationDbContext” verificar se existe a linha abaixo. Caso não exista adicioná-la:
public virtual IDbSet<Pessoa> Pessoas { get; set; }

5. Executar no Package Manager Console:
PM> add-migration versao2
PM> Update-database

6. Clicar com o botão direito na pasta “Controllers”, selecionar “Add”, selecionar ”Controller”, Sellecionar “MVC 5 Controller with views, using Entity Framework”.

7. Em “ControllerName” informar “PessoaController”. Em “Model class” selecionar “Pessoa (ProjetoIESB.Models)” e em “Data context class” selecionar “ApplicationDbContext (ProjetoIESB.Models)” e clique em “Add”

8. Clicar com o botão direito na pasta Scripts, selecione “Add”, selecione “JavaScript File”. Em “item name”  digite “CustomValidation”, clique em “Add” e insira o seguinte conteúdo:
var CustomValidation = {};
CustomValidation.Init = function () {
//CustomValidationCPF
$.validator.addMethod('customvalidationcpf', function (value, element, params) {
var cpf = value.replace(/[^0-9]/gi, ''); //Remove tudo que não for número
if (value.length == 0)
return true; //vazio
if (cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999")
return false;
add = 0;
for (i = 0; i < 9; i++)
add += parseInt(cpf.charAt(i)) * (10 - i);
rev = 11 - (add % 11);
if (rev == 10 || rev == 11)
rev = 0;
if (rev != parseInt(cpf.charAt(9)))
return false;
add = 0;
for (i = 0; i < 10; i++)
add += parseInt(cpf.charAt(i)) * (11 - i);
rev = 11 - (add % 11);
if (rev == 10 || rev == 11)
rev = 0;
if (rev != parseInt(cpf.charAt(10)))
return false;
return true; //cpf válido
}, '');
$.validator.unobtrusive.adapters.add('customvalidationcpf', {}, function (options) {
options.rules['customvalidationcpf'] = true;
options.messages['customvalidationcpf'] = options.message;
});
}
//executa -- importante que isso seja feito antes do document.ready
CustomValidation.Init();


9. Edite os arquivos Edit.chsmtl e Create.cshtml e insira/altere no final do arquivo:
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/scripts/CustomValidation.js")
}

Um comentário: