sanchaita
sanchaita

Reputation: 31

Data Annotation with Expressive Conditional Validation

I am using Expressive to implement Client side validation, following is the code, I am using conditional validation, moreover one field is dependent on other

[Required(ErrorMessage ="Role Required")]
        public string Role { get; set; }

[RequiredIf("Role == '1'", ErrorMessage = "If you plan to travel abroad, why visit the same country twice?")]
public int ProjectID { get; set; }

If role is 1 only then Project Id is compulsory, both are drop down, the issue is that the RequiredIf is not working, I get the validation message saying ProjectId is required, which is not the message that I have initialized it with, it should show me "If you plan to travel abroad, why visit the same"

Upvotes: 1

Views: 650

Answers (1)

Jeff Pitchell
Jeff Pitchell

Reputation: 61

I just ran into this same issue in my own code. You're declaring ProjectID as an int, which automatically gets a [Required()] annotation connected to it invisibly, since a type of int cannot be null.

If you change public int ProjectID to public Nullable<int> ProjectID or 'public int? ProjectID`, that should solve the issue.

Upvotes: 3

Related Questions