Let's say we have simple Object that contains two of another type
public class Parent
{
[ValidateComplexType]
public Child Child1 { get; set; }
[ValidateComplexType]
public Child Child2 { get; set; }
}
public class Child : IValidatableObject
{
public String Name { get; set; } = String.Empty
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
return new ValidationResult("Error", new[] { nameof(Name) })
}
}
I managed to do nested validation by using ObjectGraphDataAnnotationsValidator as suggested at
https://learn.microsoft.com/en-us/aspnet/core/blazor/forms-validation?view=aspnetcore-5.0#nested-models-collection-types-and-complex-types
Now let's say that I don't want Child2 to have the same Name as Child 1, so I need to compare their Name properties and display an error on the Child2 input field.
If I do this by adding IValidatableObject to the Parent and in the Validate method return new ValidationResult("Error", new[] { nameof(Child2.Name) }) this doesn't actually set the field as invalid.
I thought about adding a Func<Child, Boolean> to each child and then set it when I Instantiate the Parent object, that looks like child => child == Child2 && Child2.Name == Child1.Name and it works but it is very confusing in my opinion.
How to do this properly?
