Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a component that have an EditForm and encapsulate the form and the validation inside?

I want to make a component that have a EditForm and encapsulate the form and the validation inside of the component.

And I want to reuse this component anywhere in my application and submit it using any button.

How can I submit a EditForm from a button that is outside of it?

Observation: I have searched for other answers like this one but the answer that is marked as accepted doesn't answer the question and that is why I'm making this new question.

like image 702
Vencovsky Avatar asked Dec 06 '25 11:12

Vencovsky


1 Answers

Instead of including the EditForm in the component, create a component without the EditForm and call a component's method on OnValidSubmit

<EditForm OnValidSubmit="HandleValidSubmit">
    <FormContentComponent @ref="_formContent" />
    <button type="submit">submit</button>
</EditForm>
@code {
    private FormContentComponent _formContent;

    private void HandleValidSubmit()
    {
        _formContent.HandleValidSubmit();
    }
}
like image 178
agua from mars Avatar answered Dec 09 '25 02:12

agua from mars