Reputation: 4976
I have a very complex form in my real world application. As a matter of fact - many complex forms using complex data. There are different parts used in multiple places, that have properties needed in my form. So it looks like this:
class FormData {
[ValidateComplexType]
public PropertyClass Feature { get; } = new PropertyClass();
[Required]
public string DirectProperty { get; set; }
}
class PropertyClass {
[Required]
public string NestedProperty { get; set; }
}
Then I have a form fragment looking like this:
<TextInput @bind-Value="Input.DirectProperty" />
<TextInput @bind-Value="Input.Feature.NestedProperty />
I want to see validation messages from direct and nested properties.
It doesn't work with DataAnnotationsValidator
.
I see validation issue from the direct property, but not from the nested one. But what's truly evil - sometimes I get the validation message from the nested one. Just sometimes, and never when trying to actually submit the form with a submit button. OK, it's reproducible: I enter a valid value and press enter, I enter an invalid value and press enter or tab. Then I see the message until I click the submit button. This behavior is insane, unexpected and random.
So here comes the Microsoft's solution, meet one weird and special package:
<PackageReference Include="Microsoft.AspNetCore.Blazor.DataAnnotations.Validation" Version="3.1.0-preview4.19579.2" />
You won't search it in NuGet panel in Visual Studio. It's probably unlisted by Microsoft.
However, there is a (current?) documentation pointing to it: https://learn.microsoft.com/en-us/aspnet/core/blazor/forms-validation?view=aspnetcore-3.1
Search for the text "ValidateComplexType" or "ObjectGraphDataAnnotationsValidator".
Yes, they admit it's experimental, but first - there is no newer version of it, then again, it is unlisted. And AFAIK it's a reason for that.
The reason is it doesn't work as expected, especially after upgrading other packages.
First of all, it seems to work. I get my nested property validated, so what's the problem? After some clicking I find the same random behavior, but this time - I get duplicated validation messages. So the unexpected random behavior from the first approach displays itself with another validator.
To remove the randomness and restore sane validation behavior I must just flatten the hierarchy of the input model. Then - nothing random happens. Validations just work. The problem is - my application have many very big and complex forms. I will have to rewrite a big part of it to make random behavior disappear.
So here's the big serious question: is there a reason there is no official ObjectGraphAnnotationValidator
in Blazor? Maybe the problem of validating complex objects in Blazor forms is just too difficult to solve easily?
I don't know what to do now. Investigate how to make my own ObjectGraphAnnotationValidator
, or rewrite all input models in my application? Option A is a rabbit hole. I'm afraid I will spend a several hours on it and get some kind of random behavior again. Option B would take a couple of hours, but well, should work. So, what would you do? Go for complex type validation, or leave it, because it won't be available in Blazor this and probably next year too?
Upvotes: 6
Views: 5526