jwaliszko / ExpressiveAnnotations

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

ErrorMessage when combining RequiredIf and AssertThat #208

Open JungleMonkey opened 3 years ago

JungleMonkey commented 3 years ago

When combining RequiredIf and AssertThat the ErrorMessage from the RequiredIf is displayed when the AssertThat condition is not met.

One of the situations is this:

[DisplayName("ID Expiry")] [RequiredIf(@"IdType == 'Drivers Licence'", ErrorMessage = "Enter ID Expiry.")] [AssertThat("IdExpires >= Today()", ErrorMessage = "ID has expired")] [DataType(DataType.Date)] [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)] [ValueParser("nzdateparser")] public DateTime? IdExpires { get; set; }

If I don't enter a date in the field, I am presented with the ErrorMessage "Enter ID Expiry".

However, if I enter a date in the past I am still presented with the ErrorMessage "Enter ID Expiry" instead of "ID has expired".

If I enter a date in the future, all conditions are met and I am able to submit the form.

It is unclear whether the RequiredIf ErrorMessage is overriding the AssertThat ErrorMessage, or whether the RequiredIf is failing because the AssertThat condition has not been met and it is treating it as though it hasn't been entered at all.

How can I get "Enter ID Expiry" to show if they don't enter a date, and get "ID has expired" if they enter a date in the past?

okankocyigit commented 3 years ago

I added your properties to web-example project, it seems to be working well with your expectations. Have you checked "IdType"'s value while changing the date field?

screen-capture

JungleMonkey commented 3 years ago

Yes, it's got the right value.

If I enter a date in the future it all works. So the RequiredIf and AssertThat are passing at that point.

Interesting to note that If I use a standard Required instead of RequiredIf then I get both the "Enter ID Expiry" when nothing is entered, and the "ID has expired" when I enter a date in the past. Unfortunately I need the RequiredIf.

It's only when I use the RequiredIf that it seems to fall over.

FYI If I disable client side validation in the web.config then both error messages are returned properly. It's only the client side validation that seems to be incorrect for some reason.