0
I’m having a problem creating a custom validation using dataannotations by aspnet mvc.
My Model:
public class Usuario
{
public string Nome { get; set; }
[Idade(18)]
public string Senha { get; set; }
}
My Validation Class:
public class Idade : ValidationAttribute
{
private readonly int _idade;
public Idade(int idade)
{
_idade = idade;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if(value != null)
{
var idadeInformada = _idade;
if (idadeInformada <= 18) {
return new ValidationResult(null);
}
else
{
return new ValidationResult("Você não possui idade suficiente para se cadastrar");
}
}
return ValidationResult.Success;
}
}
The big problem is that this custom validation is not working, when I give a POST on the page it does not display the validation error, unlike when I place the validations "native" of dataannotations
really the logic was wrong, but the error persists, the message does not appear.
– William
Is your view prepared for this? You are returning Viewmodel to it after the post?
– Leandro Angelo
giving a search further sinking saw that has to be created a jquery.unobtrusive, that’s right?
– William
@William Succeeded?
– Leandro Angelo
In your way I still could not, I did a test extending the jquery validate and it worked more or less.
– William