简体   繁体   English

Angular 6-从表单外部验证并提交表单

[英]Angular 6 - Validate and submit form from outside the form

<form [formGroup]="exampleForm" (ngSubmit)="onSubmit(exampleForm)">
...
</form>

Here I'm using Angular 6 Reactive forms. 在这里,我正在使用Angular 6反应形式。

So my outside button is, 我的外部按钮是

<button mat-icon-button type="submit">
      <img src="../../../../../../assets/images/icon/save-1.svg"/>
</button>

So I want to validate the form and submit. 所以我想验证表格并提交。 And if there are validation errors save button should be disabled. 如果存在验证错误,则应禁用保存按钮。

Here is the onSubmit() implementation. 这是onSubmit()实现。

onSubmit(form : FormGroup){
this.shareObjectRequest = this.shareObjectForm.value;
if (form.value.id != null) {
  this.reportShareObjectService.put(this.reportId, this.shareObjectRequest).subscribe(
    result => {
    },
    err => {
      console.log("Error Occured." + JSON.stringify(err));
    });
}
else {
  this.reportShareObjectService.create(this.reportId, this.shareObjectRequest).subscribe(
    result => {
    },
    err => {
      console.log("Error Occured" + JSON.stringify(err));
    });
  }
}

Here is my answer. 这是我的答案。 But button is not shaded here.For that I need to use ngStyle in the button. 但是按钮在这里没有阴影。为此,我需要在按钮中使用ngStyle。

<button form="ngForm" mat-icon-button type="submit" [disabled]="myform.invalid">
      <img  src="../../../../../../assets/images/icon/save-1.svg"/>
</button>

<form [formGroup]="myform" (ngSubmit)="onSubmit(shareObjectForm)" id="ngForm">
</form>

onSubmit(form : FormGroup){
   this.shareObjectRequest = this.shareObjectForm.value;
   if (form.value.id != null) {
      this.reportShareObjectService.put(this.reportId, this.shareObjectRequest).subscribe(
         result => {
      },
         err => {
           console.log("Error Occured." + JSON.stringify(err));
         });
    }
    else {
       this.reportShareObjectService.create(this.reportId, this.shareObjectRequest).subscribe(
         result => {
         },
         err => {
            console.log("Error Occured" + JSON.stringify(err));
       });
   }
}

This is achievable in HTML5 form. 这可以HTML5形式实现。

<form id="myform" [formGroup]="registerForm" (ngSubmit)="onSubmit()">
.......
</form>

<div class="form-group">
   <button type="submit" class="btn btn-primary"  form="myform">Register</button>
</div>

Created a stackblitz https://stackblitz.com/edit/angular-pwu69r 创建了一个stackblitz https://stackblitz.com/edit/angular-pwu69r

Create Form With or without form tag. 创建带有或不带有form标签的form

Fire validation from your ts . 从您的ts验证。

submitForm() {
   for (let v in this.exampleForm.controls) {
         this.exampleForm.controls[v].markAsTouched();
   }
   if (this.exampleForm.valid) {

   }
}

And use (click) inside your html. 并在html中使用(click)

<button type="submit" class="btn btn-primary"
 (click)="submitForm()">Submit</button>
</div>

Check this Stackblitz 检查此Stackblitz

Assuming we have (simplified code) 假设我们有(简化代码)

class MyComponent {
   myForm = new FormGroup();
   doSave() {
      const form = this.myForm;
      // form.value.id
   }
}

<form [formGroup]="myForm" (ngSubmit)="doSave()">
...
</form>
<ng-container *ngIf="myForm.valid">
  your button if valid
  <button (click)="doSave()">Save</button>
</ng-container>
<ng-container *ngIf="!myForm.valid">
  your button if NOT valid
</ng-container>

Now ... 现在...

  • You can call doSave() everywhere in your view. 您可以在视图的任何地方调用doSave()
  • myForm.valid will give you the info if your form is valid. 如果您的表格有效, myForm.valid将为您提供信息。 You can use two separate sections as in my example if [disabled]="myForm.valid" does not work for you. 如果[disabled]="myForm.valid"对您不起作用,则可以使用两个单独的部分作为示例。

If you already have done the FormGroup in the component and thats what you are binding to in the template you just need to reference the FormGroup again in the disabled attr. 如果您已经在组件中完成了FormGroup,而这就是您要在模板中绑定的内容,则只需在禁用的attr中再次引用FormGroup。

Even when in my example the button is inside form that's irrelevant, 'cause you are using the a variable declared in the component as a reference, therefore you could use it where ever in your html 即使在我的示例中,按钮位于无关紧要的表单内部,因为您使用的是在组件中声明的变量作为引用,因此您可以在html中的任何位置使用它

<form autocomplete="off" [formGroup]="actualFormGroup" (ngSubmit)="submitForm()" class="data-form">
  ...

  <div class="button-wrapper">
    <button class="classic-purple-button cursor-pointer no-outline" [disabled]="!actualFormGroup.valid" type="submit">
        Continuar
    </button>
  </div>
</form>

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

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