Closed jeremysalwen closed 10 months ago
@jeremysalwen thanks for contacting us.
Unfortunately, the semantics for the required attribute aren't "this property was set". RequiredAttribute only checks for null values and empty strings if configured to do so.
In your case, you can either use a nullable Enum or create your own validation attribute. In your case, you should use
[EnumDataType(typeof(SomeEnum))]
to ensure the value was set.
The logic for the required attribute is here. We don't do any processing nor special casing based on it at the Blazor level.
This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.
See our Issue Management Policies for more information.
Is there an existing issue for this?
Describe the bug
When using an required enum property with an input select, the validation is mistakenly not performed:
Will not validate the required value, and instead silently sets it to the invalid enum value 0.
This bug was previously reported but mistakenly closed: Blazor's InputSelect does not validate with Required attribute The bug is still present in .NET 8.
Expected Behavior
I expect the validation to fail since a Required property is not set, i.e. the behavior of any other input element.
Steps To Reproduce
Exceptions (if any)
No response
.NET Version
8.0.100
Anything else?
No response