简体   繁体   English

如何制作一个有 EditForm 的组件并将表单和验证封装在里面?

[英]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.我想制作一个具有EditForm的组件,并在组件内部封装表单和验证。

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?如何从它外部的按钮提交EditForm

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 ,而是创建一个没有EditForm的组件并在OnValidSubmit上调用组件的方法

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

    private void HandleValidSubmit()
    {
        _formContent.HandleValidSubmit();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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