0

I am trying to call the validate(ngNativeValidate) function on click of button.

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. I want the same behavior even for TEST CONNECTION button. 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.

testEndpoint is a backend call function.

1
  • (keydown)="testEndpoint(myForm.value);" so keydown event you can check the connection one way Commented Feb 4, 2019 at 10:44

2 Answers 2

0

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Indeed this is one way, which is available everywhere on internet. But while form submit I do not want to write any span code. In default behavior, the focus goto exactly at the same place of problem. I want exactly that behavior.
0

I think here is a solution: Trigger Input Validation

So, follow this link, you can try this: form.controls['myControl'].updateValueAndValidity();.

You can "validate" the complete form, too. Like: this.myForm.markAllAsTouched();

Have fun!

Greetings, Florian

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.