Blazor Form Validation on Nested Objects has no Cycle Detection
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 5h
- Merged PRs (30d)
- 276
Description
### Is there an existing issue for this?
- [x] I have searched the existing issues
### Describe the bug
I am trying to use Blazor EditForm Validation for some deeply nested DDD Aggregates. I am following [this](https://learn.microsoft.com/en-us/aspnet/core/blazor/forms/validation?view=aspnetcore-10.0#nested-objects-and-collection-types) doc article. Since I am using ef core for persistence, I usually have a reference to the parent object inside the nested object. When I deal with validation of my actual Aggregates it appears that no validation is happening at all. When I remove the reference to the parent object validation is working fine.
### Expected Behavior
I expect the framework to deal with detecting cycle reference and thus correct validation of the aggregate and all nested entities. At least a helpful error message shall be thrown.
### Steps To Reproduce
Edit the example given in the [docs](https://learn.microsoft.com/en-us/aspnet/core/blazor/forms/validation?view=aspnetcore-10.0#nested-objects-and-collection-types) in the following way:
```
using System.ComponentModel.DataAnnotations;
[ValidatableType]
public class Order
{
public Customer Customer { get; set; } = new();
public List OrderItems { get; set; } = [];
}
public class Customer
{
// Add the navigation to the parent object to produce a cycle
public Order Order {get; set;}
[Required(ErrorMessage = "Name is required.")]
public string? FullName { get; set; }
[Required(ErrorMessage = "Email is required.")]
public string? Email { get; set; }
public ShippingAddress ShippingAddress { get; set; } = new();
}
```
```
Customer Details
Full Name
@* Add a submit button so you can see the numerous validation error messages *@
Submit
@code {
public Order? Model { get; set; }
// Edit the onitialized to add the cycle (as ef core would materialize the aggregate)
//protected override void OnInitialized() => Model ??= new();
protected override void OnInitialized()
{
Model = new();
Model.Customer.Order = Model;
}
}
```
This gives the following result:
As described earlier, when the aggregate becomes big, no validations are shown and it looks like there is no validation happening at all.
### Exceptions (if any)
Unfortunately, no Exceptions are thrown.
### .NET Version
10.0.102
### Anything else?
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.