jwaliszko / ExpressiveAnnotations

Annotation-based conditional validation library.
MIT License
351 stars 123 forks source link

RequiredIf Attribute not working - 2.7.4 #199

Closed ninzacoder closed 5 years ago

ninzacoder commented 5 years ago

I have the following validation not working and cannot resolve why that is the case?

    [Display(Name = "Bus")]
    [RequiredIf("SelectedWidgetText == 'Referral'", ErrorMessage = "Please select a Vehicle.")]
    public int DepotId{ get; set; }

    public string SelectedWidgetText { get; set; }

I have also tried the following:

[Display(Name = "Bus")]
[AssertThat("SelectedWidgetText == 'Referral'", ErrorMessage = "Please select a Vehicle.")]
public int DepotId{ get; set; }

on the server side, when the modelstate.isvalid method is hit, it returns false and the error message thrown is The DepotId field is required.

ninzacoder commented 5 years ago

just by changing the DepotId to Nullable property like the following, the validation started working as expected:

public int? DepotId{get;set;}