Check two null fields with Data Annotation

4

I have two properties:

public int? Inicio {get; set;}
public int? Final {get; set;}

Both accept nulls and are not required. I wanted to check if the two are null using Data Annotation . The operation would look something like Compare Data Annotation. , but checking if the value of the two properties are null.

    
asked by anonymous 03.12.2015 / 21:11

2 answers

6

When it involves two fields, the correct one is to decorate your Model with IValidatableObject " as follows:

public class MeuModel : IValidatableObject
{
    public int? Inicio {get; set;}
    public int? Final {get; set;}

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        /* Verifique aqui as variáveis.
           Em caso de problemas, devolva erros usando:
           yield return new ValidationResult("Mensagem de erro.", new[] { "CampoEnvolvido1", "CampoEnvolvido2" });
        */
    }
}
    
03.12.2015 / 21:21
0

You can create a new notation for this so I sent below how you will override the is valid method

    
08.12.2015 / 17:18