如何制作一个具有EditForm的组件并在其中封装表单和验证? [英] How to make a component that have an EditForm and encapsulate the form and the validation inside?

查看:342
本文介绍了如何制作一个具有EditForm的组件并在其中封装表单和验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个具有EditForm的组件,并将表单和验证封装在该组件内部.

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.

如何通过其外部的按钮提交EditForm?

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.

推荐答案

而不是在组件中包含EditForm,而是创建一个没有EditForm的组件,并在OnValidSubmit

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

这篇关于如何制作一个具有EditForm的组件并在其中封装表单和验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆