I need to display validation messages if a nested-component is not properly filled in. The component is consumed by other parent-components and they need to get feedback on whether there are validation issues.
I have tried the following code for the nested-component and used the CanSubmit
method. While the method correctly tells if there are validation problems the validation messages are not showing.
All the code below can be tested on blzorrepl: https://blazorrepl.com/repl/GvOQlvvv1789ra1G37
@if(editContext != null) {
<EditForm EditContext="@editContext">
<input type="text" @bind="testModel.Name" />
<DataAnnotationsValidator />
<ValidationSummary />
</EditForm>
}
@code {
[Parameter]
public TestModel testModel
{
get { return (TestModel)editContext?.Model; }
set { editContext = new EditContext(value); }
}
EditContext editContext;
public bool CanSubmit()
{
return editContext.Validate();
}
}
This is my parent-component code, a bit reduced but reproducing the problem:
<ChildComponent @ref="myTestComponent" testModel="testModel" />
<input type="button" @onclick="buttonClick" value="validate programmatically" />
<div>@testMessage</div>
@code {
TestModel testModel = new TestModel();
ChildComponent myTestComponent;
string testMessage;
void buttonClick()
{
testMessage = "not passed validation";
if (myTestComponent.CanSubmit())
{
testMessage = "passed validation!";
}
}
}
The testMessage
is only used to show the validation status.
What can I do to make parent-component cause the nested-component to show validation messages? I can only place the submit
in the parent-component.
As it was requested, here is a more complete example of what I am doing, a list of items which can be edited inline as well as a form to add more instances. https://blazorrepl.com/repl/mlYwlQPm34bekYE824