How do I make a date annotation, can access value of another property?

2

I have my following properties:

public ETipoPessoa TipoPessoa {get;set;}
public string CnpjCPF {get;set;}

public enum ETipoPessoa {
     Fisica,
     Juridica
}

I have this condition, if TypePerform = Physical, I need to pass an annotation that validates the CPF, otherwise TypePerform = Juridica validates the CNPJ

So, how do I make a date annotation, access value of another property?

That is, my annotation has to get the value that came from the TypePerson, to validate what is what ...

    
asked by anonymous 17.09.2014 / 03:35

1 answer

3

It's not what you want, but there's a way to do Template validation.

public class Pessoa : IValidatableObject
{
    public ETipoPessoa TipoPessoa {get;set;}
    public string CnpjCPF {get;set;}

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        var validations = new List<ValidationResult>();

        if (TipoPessoa == ETipoPessoa.Fisica)
        {
            bool cpfValido;
            ... // validação de CPF
            if (!cpfValido)
                validations.Add(new ValidationResult("CPF Inválido"));
        }

        if (TipoPessoa == ETipoPessoa.Juridica)
        {
            bool cnpjValido;
            ... // validação de CNPJ 
            if (!cnpjValido)
                validations.Add(new ValidationResult("CNPJ Inválido"));
        }
        return validations;
    }
}
    
17.09.2014 / 16:29