Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need to validate nested components in EditForm #60127

Open
Bond-Addict opened this issue Jan 30, 2025 · 6 comments
Open

Need to validate nested components in EditForm #60127

Bond-Addict opened this issue Jan 30, 2025 · 6 comments
Labels
area-blazor Includes: Blazor, Razor Components

Comments

@Bond-Addict
Copy link

This is POC repo which demonstrates the issue and what I've tried so far
https://github.com/[Bond-Addict/NestedValidation](https://github.com/Bond-Addict/NestedValidation)

I've tried several different validation libraries with no luck. This would seem to be a common use-case but I've been able to find a solution.

Issue:

When using a EditForm in a parent component, child components do not emit their validation state up to parent.

Current Attempt

Registering the EditContext as a CascadingParameter on the child. When validation is requested from the parent try and update the EditContext message store to include the invalid fields from the child.

Info:

The validators both work and validate as expected and the child component does correctly get notified when validation is requested on the EditContext.

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-blazor Includes: Blazor, Razor Components label Jan 30, 2025
@MariovanZeist
Copy link
Contributor

Hi @Bond-Addict your repo doesn't appear to exist. Did you set it to private?

@Bond-Addict
Copy link
Author

Yup, my bad. It's public now

@MariovanZeist
Copy link
Contributor

@Bond-Addict nested properties should be validated in your case. The issue was that you didn't set a rendermode
resulting in the home page being recreated every time you press Submit.
If you change the following in your App.razor

 <HeadOutlet />
 ...
 <Routes />

To

 <HeadOutlet @rendermode="InteractiveServer" />
 ...
 <Routes @rendermode=InteractiveServer />

Your application will work. You can read more about Rendermodes here

You can simplify your code by getting rid of everything in the AddressComponent.razor file beneath the

[Parameter]
public Address Address { get; set; }

@Bond-Addict
Copy link
Author

@Bond-Addict nested properties should be validated in your case. The issue was that you didn't set a rendermode
resulting in the home page being recreated every time you press Submit.
If you change the following in your App.razor

 <HeadOutlet />
 ...
 <Routes />

To

 <HeadOutlet @rendermode="InteractiveServer" />
 ...
 <Routes @rendermode=InteractiveServer />

Your application will work. You can read more about Rendermodes here

You can simplify your code by getting rid of everything in the AddressComponent.razor file beneath the

[Parameter]
public Address Address { get; set; }

I'll check that out shortly. Thank you!

@Bond-Addict
Copy link
Author

Bond-Addict commented Jan 31, 2025

So even after that change, the address component isn't validated. I updated the repo just in case I misunderstood the instructions.

@MariovanZeist
Copy link
Contributor

@Bond-Addict I spotted my error, I missed the fact that the address wasn't validated when pressing the submit button, (after the above change it was correctly validating the individual fields while editing, but not when pressing the submit button).

When pressing submit the EditContext validates only what's in the Model property, the actual validation is handled in this case by the FluentValidation library. If you want FluentValidation to validate child properties you have to specify it.

By adding the following code inside the PersonValidator

  RuleFor(p => p.Address).SetValidator(new AddressValidator());

The address will now also be validated when pressing submit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

No branches or pull requests

2 participants