简体   繁体   中英

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.

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM