dotnet/aspnetcore
Voir sur GitHubValidationMessageStore.Add field accessor parameter shows a warning for nullable properties
Open
#41 338 ouverte le 23 avr. 2022
Pillar: Technical DebtPriority:2area-blazorbugfeature-blazor-form-validationhelp wanted
Métriques du dépôt
- Stars
- (37 933 stars)
- Métriques de merge PR
- (Merge moyen 16j 9h) (258 PRs mergées en 30 j)
Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
Adding validation errors using the below ValidationMessageStore.Add method triggers a warning when nullable annotations are enabled for the project, and the property referenced in the accessor parameter has a nullable type.
Expected Behavior
No warnings are displayed for properties with nullable type.
Steps To Reproduce
The following code shows how the warning message is triggered:
private TestModel model = new();
private class TestModel
{
public string? Text { get; set; }
}
private void ManualValidation()
{
var messageStore = new ValidationMessageStore(editContext);
if (true /* External validation rule */)
{
messageStore?.Add(() => model.Text, "This value is not valid");
// ^^^^^^^^^^ <-- Warning triggered here
}
}
Exceptions (if any)
No response
.NET Version
6.0.202
Anything else?
Changing the method signature to the following (from to generic parameter objectTField) could fix the issue as far as I understand:
public void Add<TField>(Expression<Func<TField>> accessor, string message)
That would match the the method in FieldIdentifier: