Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ErrorMessage when combining RequiredIf and AssertThat #208

Open
JungleMonkey opened this issue Jan 16, 2021 · 2 comments
Open

ErrorMessage when combining RequiredIf and AssertThat #208

JungleMonkey opened this issue Jan 16, 2021 · 2 comments

Comments

@JungleMonkey
Copy link

JungleMonkey commented Jan 16, 2021

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
Copy link

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants