Angular2 - Validate and submit form from outside

后端 未结 8 2020
一个人的身影
一个人的身影 2020-11-30 22:44

I have a simple form that looks like this

...

and need to

相关标签:
8条回答
  • 2020-11-30 23:04

    Below solution is working in my case, please try this simple solution. I am not sure, if it will be working into all the conditions:

    <form #documentEditForm="ngForm" id="ngForm" (ngSubmit)="save(documentEditForm.valid)"> 
        ...Your Input Elements... 
    </form>
    

    The button should be declared outside the form like this:

    <button form="ngForm">Submit</button>
    

    The validation of the form should check into save() by following conditions

    save(isValid:boolean){
        if(isValid) {
            ...Your code to save details...
        }
    }
    

    Hope this simple solution will help you.

    0 讨论(0)
  • 2020-11-30 23:05

    The correct way of doing is actually

    <form (ngSubmit)="save()" id="ngForm" #documentEditForm="ngForm"> 
        ... 
    </form>
    
    <button class="btn-save button primary" form="ngForm" [disabled]="!documentEditForm.form.valid">
        SAVE
    </button>
    

    The form needs to have an ID id="example-form" and the submit button a matching ID in the form="example-form"

    0 讨论(0)
提交回复
热议问题