Angular 8反应形式验证

时间:2020-08-15 20:01:25

标签: angular typescript angular8 angular-reactive-forms

我在 CustomerAdd.html 中使用反应式表单。

<form [formGroup]="CustomerProfileModel.FormCommonGroup">
        <div class="col-12 tab-content">
            <div class="row">
              <div class="col-md-10">
                <div class="form-group row m-b-10 m-t-20">
                  <label class="col-2 text-md-right col-form-label">Legal Name *</label>
                  <div class="col-4">
                    <input type="text" name="CommonLegalNameControl" required minlength="4" placeholder="" class="form-control" formControlName="CommonLegalNameControl" [(ngModel)]="CustomerProfileModel.LegalName">
                    <div *ngIf="CommonLegalNameControl?.invalid && (CommonLegalNameControl?.dirty || CommonLegalNameControl?.touched)" class="alert alert-danger">
                      <div *ngIf="CommonLegalNameControl?.errors.required">
                        Name is required.
                      </div>
                      <div *ngIf="CommonLegalNameControl?.errors.minlength">
                        Name must be at least 4 characters long.
                      </div>
                      <div *ngIf="CommonLegalNameControl.errors.forbiddenName">
                        Name cannot be Bob.
                      </div>
                    </div>
                  </div>
                </div>
                <!-- end form-group row -->
                <!-- begin form-group row -->
                <div class="form-group row m-b-10">
                  <div class="col-md-4 offset-md-2">
                    <!-- <input class="btn btn-sm btn-primary m-r-5" [disabled]="!(CustomerProfileModel.FormCommonGroup.valid)" (click)="AddCompany()" type=button value="Add Customer" /> -->
                    <input class="btn btn-sm btn-primary m-r-5" (click)="AddCompany()" type=button value="Add Customer" />
                    <a [routerLink]="['/CustomerProfile']" class="btn btn-sm btn-default">Cancel</a>
                  </div>
                </div>
              </div>
              <!-- end panel -->
            </div>                
          </div>
        </div>
      </form>

这是我的模型文件 Customer.ts

import {
  NgForm,
  FormGroup,
  FormControl,
  Validators,
  FormBuilder
} from '@angular/forms'

export class Customer {
  LegalName: string = "";
  FormCommonGroup: FormGroup = null;
  constructor() {
    var _builder = new FormBuilder();
    this.FormCommonGroup = _builder.group({}); //Use the builder to create

    //control --> validation and 1 validation
    this.FormCommonGroup.addControl("CommonLegalNameControl",
      new FormControl('', Validators.required));
  }
}

正如我在下面在 CustomerAdd.html 上所说的那样。

<input type="text" name="CommonLegalNameControl" required minlength="4" placeholder="" class="form-control" formControlName="CommonLegalNameControl" [(ngModel)]="CustomerProfileModel.LegalName">
                <div *ngIf="CommonLegalNameControl?.invalid && (CommonLegalNameControl?.dirty || CommonLegalNameControl?.touched)" class="alert alert-danger">
                  <div *ngIf="CommonLegalNameControl?.errors.required">
                    Name is required.
                  </div>
                  <div *ngIf="CommonLegalNameControl?.errors.minlength">
                    Name must be at least 4 characters long.
                  </div>
                  <div *ngIf="CommonLegalNameControl.errors.forbiddenName">
                    Name cannot be Bob.
                  </div>
                </div>

这似乎不起作用,我对 click 方法也遵循了同样的要求。
https://stackblitz.com/edit/angular-7-reactive-form-validation?file=app%2Fapp.component.html
https://stackblitz.com/edit/angular-8-reactive-form-validation?file=app%2Fapp.component.html
他们两个都无法按我的形式正常工作。

更新1: 这是下面的我的CustomerAdd.ts文件。

import { Component, Input, OnInit, ViewEncapsulation } from '@angular/core';
import { Customer } from '../_models/Customer '
@Component({
  selector: 'CustomerProfileAdd',
  templateUrl: './CustomerProfileAdd.html',
  styleUrls: ['./CustomerProfileAdd.css'],
})

export class CustomerProfileAdd implements OnInit {
  public CustomerProfileModel: Customer = new Customer();
  constructor() {
  }

  async ngOnInit() {    
    await this.CustomerProfileModel;
  }

  AddCompany() {
    this.submitted = true;

    // stop here if form is invalid
    if (this.CustomerProfileModel.FormCommonGroup.invalid) {
      return;
    }
  }
}

1 个答案:

答案 0 :(得分:1)

当我再次遵循此网址时。
https://stackblitz.com/edit/angular-8-reactive-form-validation?file=app%2Fapp.component.html
现在,在我完成工作的过程中,我错过了一些要点。

 <input type="text" formControlName="lastName" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.lastName.errors }" />
                    <div *ngIf="submitted && f.lastName.errors" class="invalid-feedback">
                        <div *ngIf="f.lastName.errors.required">Last Name is required</div>
                    </div>

我所做的一切都还可以,因为我错过了这一部分。 [ngClass]="{ 'is-invalid': submitted && f.lastName.errors }"是在输入标签中提供的,以便正常工作。在 CustomerAdd.ts 中,我提供了此地址。

// convenience getter for easy access to form fields
    get f() { return this.CustomerProfileModel.FormCommonGroup.controls; }