Created
June 22, 2011 15:47
-
-
Save waldyrfelix/1040391 to your computer and use it in GitHub Desktop.
Exemplo de CustomValidation aplicado
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel.DataAnnotations; | |
using System.Linq; | |
namespace AppWeb.ViewModels | |
{ | |
public class LoginViewModel | |
{ | |
[Required(ErrorMessage = "Login é um campo obrigatório")] | |
[StringLength(30, ErrorMessage = "O login deve ter no máximo 30 caracteres")] | |
[CustomValidation(typeof(ValidacaoDoLoginViewModel), "PermitirLogin")] | |
public string Login { get; set; } | |
[Required(ErrorMessage = "Senha é um campo obrigatório")] | |
[StringLength(16, MinimumLength = 8, ErrorMessage = "A senha deve ter entre 8 e 16 caracteres")] | |
[RegularExpression(@"[a-zA-Z0-9]*", ErrorMessage = "Na senha somente são permitidos caracteres alfanuméricos")] | |
public string Senha { get; set; } | |
} | |
public class ValidacaoDoLoginViewModel | |
{ | |
private static List<string> blackList = new List<string> { | |
"waldyr.felix", | |
"bilbo.bolseiro" | |
}; | |
public static ValidationResult PermitirLogin(string login, ValidationContext validationContext) | |
{ | |
if (blackList.Any(b => b == login)) | |
{ | |
string mensagem = String.Format("{0} não pode logar porque está na black list", login); | |
return new ValidationResult(mensagem, new[] { "Login" }); | |
} | |
return ValidationResult.Success; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment