FluentValidation icon indicating copy to clipboard operation
FluentValidation copied to clipboard

[Bug] OnFieldChanged validates field using validator for owning instance type, not the one for EditContext.Model type

Open hakenr opened this issue 1 year ago • 5 comments

Describe the bug Assume we have a complex type from this documentation: https://docs.fluentvalidation.net/en/latest/start.html#complex-properties
There is a Customer class with an Address property and two validators: CustomerValidator and AddressValidator.

Let’s remove this line from the CustomerValidator:

public class CustomerValidator : AbstractValidator<Customer> 
{
  public CustomerValidator()
  {
    RuleFor(customer => customer.Name).NotNull();
-   RuleFor(customer => customer.Address).SetValidator(new AddressValidator());
  }
}

The AddressValidator still exists:

public class AddressValidator : AbstractValidator<Address> 
{
  public AddressValidator()
  {
    RuleFor(address => address.Postcode).NotNull();
    // etc.
  }
}

Now, when you create this type of UI (shortened for brevity):

<EditForm Model="company">
  <FluentValidationValidator />
  <InputText @bind-Value="company.Address.Postcode" />

The FluentValidationValidator validates the Postcode field using the AddressValidator, even though the CompanyValidator for the Model does not reference the AddressValidator, and companyValidator.Validate() does not use the AddressValidator. The Postcode field should not be validated in this case.

Expected behavior The validator for the Model (CustomerValidator) should be used for all field validations, company.Address.Postcode should not be validated as CustomerValidator does not reference AddressValidator.

Additional context https://github.com/Blazored/FluentValidation/blob/f02c9beb6489574b91bb20c2a2257a6f2339eb67/src/Blazored.FluentValidation/EditContextFluentValidationExtensions.cs#L84-L105
I believe the issue is in how the ValidateField() method is implemented. It uses fieldIdentifier.Model directly and does not take editContext.Model into account. The implementation won’t be trivial (you cannot simply replace fieldIdentifier.Model with editContext.Model as someone has already tried), but it is doable.

Related items:

  • #76
  • #91
  • #104
  • #132
  • #178

cc @jirikanda

hakenr avatar Sep 24 '24 16:09 hakenr

Also seems to be related:

  • #204

hakenr avatar Oct 29 '24 11:10 hakenr

Same issue for #245

Bond-Addict avatar Feb 04 '25 21:02 Bond-Addict

@chrissainty there have now been at least 5 issues opened for/related to this in the last 6 months. #235, #205, #238, #245 and most recently #247. This is a bug that's makes the library non-functional. Please look into this at your earliest convenience. This has 2 of our production apps down/limited functionality atm.

Bond-Addict avatar Feb 13 '25 16:02 Bond-Addict

Bump, having the same issue on one of our projects.

Had to downgrade the package until this is fixed.

AdamGovier avatar Oct 07 '25 14:10 AdamGovier

Important Notice from Contributor

Unfortunately, I'm no longer able to maintain this project due to access issues. Rather than trying to patch things up, I've decided to channel my efforts into a complete rewrite.

Introducing Blazilla — a fresh take on FluentValidation for Blazor. This new project was built from the ground up to address many of the lingering issues in the original implementation. If you're looking for a modern, actively maintained solution, I encourage you to check it out.

These issues should be fixed in Blazilla.

🔗 GitHub – Blazilla

Thanks for your support! ~ Paul

pwelter34 avatar Oct 08 '25 00:10 pwelter34