Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it possible to validate child components?

See original GitHub issue

I’ve this Claim submit razor page:

<EditForm Model="@Model" OnValidSubmit="@Submit">
    <FluentValidationValidator />

    <div class="row">
        <AddressPartial Model="@Model" />

And this Address child page:

@inject ISharedResources Localizer

<LabelText For="@(() => Model.Address.Street)" />
<div class="input-group mb-3">
    <InputText @bind-Value="@Model.Address.Street" />
    <ValidationMessage For="@(() => Model.Address.Street)" />

@code {
    public ClaimBase Model { get; set; }

And when I clear the street (which should result in an required error message), this does not work and I get this exception in the browser:

[2019-09-26T18:09:51.529Z] Error: System.InvalidCastException: Unable to cast object of type 'Models.Address' to type 'Models.Claim'.
   at FluentValidation.ValidationContext.ToGeneric[T]() in /home/jskinner/code/FluentValidation/src/FluentValidation/ValidationContext.cs:line 211
   at FluentValidation.AbstractValidator`1.FluentValidation.IValidator.ValidateAsync(ValidationContext context, CancellationToken cancellation) in /home/jskinner/code/FluentValidation/src/FluentValidation/AbstractValidator.cs:line 74
   at Blazored.FluentValidation.EditContextFluentValidationExtensions.ValidateField(EditContext editContext, ValidationMessageStore messages, FieldIdentifier fieldIdentifier, IServiceProvider serviceProvider, IValidator validator) in C:\Users\Web.Blazor\Validation\EditContextFluentValidationExtensions.cs:line 64

Is this supposed to work, or should the below code be changed into:

 validator = GetValidatorForModel(serviceProvider, fieldIdentifier.Model); // fieldIdentifier.Model = the Address and editContext.Model is the Claim
private static async void ValidateField(EditContext editContext, ValidationMessageStore messages, FieldIdentifier fieldIdentifier, IServiceProvider serviceProvider, IValidator validator = null)
            var properties = new[] { fieldIdentifier.FieldName };
            var context = new ValidationContext(fieldIdentifier.Model, new PropertyChain(), new MemberNameValidatorSelector(properties));

            if (validator == null)
                validator = GetValidatorForModel(serviceProvider, editContext.Model);

            var validationResults = await validator.ValidateAsync(context);

            messages.Add(fieldIdentifier, validationResults.Errors.Select(error => error.ErrorMessage));


Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:18 (11 by maintainers)

github_iconTop GitHub Comments

chrissaintycommented, Sep 27, 2019

We’re using this control at work and we validate many nested models without any issues. I’m not saying what you’re suggesting is wrong but I’m just trying to understand why you have an issue and we don’t. Thanks for providing a repro, I’ll take a look at this over the weekend and get back to you.

rblancacommented, Mar 23, 2023

same here…same issue .Net 7

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to validate child form from parent component in Vue
In the child component, you need to monitor the change in the props value via watch and call your validation function. // Parent...
Read more >
Optionally do not validate child components · Issue #719
My use case is, that child component is modal dialog and my parent component has another form completely unrelated. Now when I am...
Read more >
Child component validation on Parent Component's Save
Child component has a lot of validations. These validations would fire on 'blur' event of input* fields (say inputNumber) fields in child ......
Read more >
Custom Validations between Child, Parent and ...
In this blog you will learn how you may use validation between Child, Parent and Grandparent component. The requirement was to have a...
Read more >
Custom Validations between Child, Parent and ... - YouTube
In this video you will learn how you may use custom validation with LWC compositions. In the example I am having three components...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found