简体   繁体   English

在使用ngNativeValidate提交表单之前,先调用validate函数

[英]Calling validate function before submiting the form using ngNativeValidate

Hi I am trying to call the validate(ngNativeValidate) function on click of button. 嗨,我试图在单击按钮时调用validate(ngNativeValidate)函数。

My form look like: 我的表格如下:

<form #myForm="ngForm" (ngSubmit)="addEndpoint(myForm.value);" ngNativeValidate>
        <section class="form-block">
            <div class="form-group">
                <input type="text" placeholder="name" name="name" [(ngModel)]="myData.name" [hidden]="true">
            </div>
            </section>
        <button type="button" class="btn btn-outline" (click)="testEndpoint(myForm.value);">TEST CONNECTION</button>
        <button type="submit" class="btn btn-primary" [disabled]="disableSubmit">SUBMIT</button>
    </form>

Validation works as expected for SUBMIT button. 验证按“ SUBMIT按钮的预期进行。 I want the same behavior even for TEST CONNECTION button. 即使是“ TEST CONNECTION按钮,我也想要相同的行为。 but don't want to submit the form. 但不想提交表格。 How I can do it? 我该怎么办? I think there will be some way to just call the validator function on-click event. 我认为将有某种方式可以仅调用验证器函数的单击事件。 Any help is appreciated. 任何帮助表示赞赏。 Thanks! 谢谢! in advance. 提前。

EDIT: testEndpoint is a backend call function. 编辑: testEndpoint是一个后端调用函数。

In reactive forms you can use the 'valid' and 'touched' properties of the form to display feedback to the user if what you are trying to do with 'Test connection' button is to check user inputs. 在反应式表单中,如果您要使用“测试连接”按钮尝试检查用户输入,则可以使用表单的“有效”和“触摸”属性向用户显示反馈。

Therefore, you could skip the 'test connection' button and directly output the following span if the form is invalid and the button submit disabled: 因此,如果表单无效且按钮提交已禁用,则可以跳过“测试连接”按钮并直接输出以下范围:

<button type="submit" class="btn btn-primary" [disabled]="disableSubmit">SUBMIT</button>
<span *ngIf="!myForm.valid && myForm.touched" class="help-block">Please enter valid data!</span>

Hope it helps! 希望能帮助到你!

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

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