Is it possible to validate child components?
See original GitHub issueI’ve this Claim submit razor page:
<EditForm Model="@Model" OnValidSubmit="@Submit">
<FluentValidationValidator />
<div class="row">
<AddressPartial Model="@Model" />
</div>
</EditForm>
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)" />
</div>
@code {
[Parameter]
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.Clear(fieldIdentifier);
messages.Add(fieldIdentifier, validationResults.Errors.Select(error => error.ErrorMessage));
editContext.NotifyValidationStateChanged();
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:18 (11 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
same here…same issue .Net 7