Blazored / FluentValidation

A library for using FluentValidation with Blazor
https://blazored.github.io/FluentValidation/
MIT License
587 stars 84 forks source link

ValidateField: fieldIdentifier.Model different to editContext.Model #178

Open LukeTOBrien opened 1 year ago

LukeTOBrien commented 1 year ago

Is your feature request related to a problem? Please describe. I have a form where some of my controls are bound to properties in my code-behind or VM and not to the POCO EditForm model.

This means that for those fields validation does not happen when the OnFieldChanged event is fired but only when the form is submitted.
The issue is in the ValidateField method:

...
var context = new ValidationContext<object>(/* Here -> */fieldIdentifier.Model, new PropertyChain(), new MemberNameValidatorSelector(properties));

validator ??= GetValidatorForModel(serviceProvider, /* Here -> */fieldIdentifier.Model, disableAssemblyScanning);
...

Describe the solution you'd like I think a boolean property on the Validator component would do the trick:

<FluentValidationValidator UseContextModel="true" />

then in code:

...
var context = new ValidationContext<object>(UseContextModel ? editContext.Model : fieldIdentifier.Model, new PropertyChain(), new MemberNameValidatorSelector(properties));

validator ??= GetValidatorForModel(serviceProvider, UseContextModel ? editContext.Model : fieldIdentifier.Model, disableAssemblyScanning);
...

However the AddFluentValidation extension method doesn't have a reference to the component, so that method would need to be modified to pass in the component.

What do you think?
Do you understand my issue?

LukeTOBrien commented 1 year ago

@chrissainty what do you think about this?
I would be willing to sponsor any change.

chrissainty commented 1 year ago

Hi @LukeTOBrien, I have a new baby so I'm not going to make any promises on when I can get an update done.

I would like to clarify what you're attempting. Am I right in thinking you have a form but some fields in the form are not part of the model you've provided to EditForm?

If so, this isn't really how EditForm and the underlying EditContext were designed to work. Can you provide some example code of what you're currently doing.

LukeTOBrien commented 1 year ago

Hi @chrissainty, congratulations on the new baby! I understand that takes priority.

Yes you are correct, I have a POCO object which I validate and post to the API for updating to the database, so the EditForm and my validators are using the POCO class.
But the view has a ViewModel and the Input fields are bound to this, infact inside the form I have many children components each with their own ViewModel.
So the ViewModels update the POCO and validation happens on submit, but because the Input fields are bound to the ViewModels so the fieldIndentifier.Model doesn't match the the validator so validation doesn't happen OnChange.

I will try to create a minimal repo to demonstrate, and perhaps rethink how I am doing thing.