문제

I ValidationAttribute below, which validates the information in two fields. It works, but always doing post. In client-side is not working.

What should I switch to ValidationAttribute work on client-side?

[AttributeUsageAttribute(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
public class SexoComTipoPessoa : ValidationAttribute
{
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        var property = validationContext.ObjectInstance.GetType().GetProperty("TipoPessoaID");

        if (property == null)
            return new ValidationResult("Propriedade desconhecida: 'TipoPessoaID'");

        var propertyValue = property.GetValue(validationContext.ObjectInstance, null);

        /* Tipo de pessoa 2 - é pessoa juridicia */
        if (Convert.ToInt32(propertyValue) == 2)
        {
            if (Convert.ToInt32(value) != 3)
                return new ValidationResult("Para o tipo de pessoa 'Juridíca', deve ser selecionado o sexo 'Não aplicavél'!");
        }
        else if (Convert.ToInt32(propertyValue) == 1)
        {
            if (Convert.ToInt32(value) == 3)
                return new ValidationResult("Para o tipo de pessoa 'Física', deve ser selecionado o sexo 'Masculino' ou 'Feminino'!");
        }

        return ValidationResult.Success;
    }
}
도움이 되었습니까?

해결책

Do you need create a method in jquery.validation for this.

Look this example for helping:

Jquery Validation - Condition Method

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top