0

在实现我的代码时,我在控制台中遇到以下错误

错误:在 FormGroupDirective.push../node_modules/@angular/forms/fesm5/forms.js.FormGroupDirective 的 setUpControl (forms.js:1640) 的 _throwError (forms.js:1732) 找不到名称为“密码”的控件.addControl (forms.js:4454) 在 FormControlName.push../node_modules/@angular/forms/fesm5/forms.js.FormControlName._setUpControl (forms.js:4959) 在 FormControlName.push../node_modules/@angular /forms/fesm5/forms.js.FormControlName.ngOnChanges (forms.js:4909) 在 checkAndUpdateDirectiveInline (core.js:9244) 在 checkAndUpdateNodeInline (core.js:10512) 在 checkAndUpdateNode (core.js:10474) 在 debugCheckAndUpdateNode (core .js:11107) 在 debugCheckDirectivesFn (core.js:1106

我试图通过分组密码和重新密码控制来创建一个新的表单组,但它对我不起作用。

添加组织.ts

import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';


@Component({
  selector: 'app-add-organization',
  templateUrl: './add-organization.component.html',
  styleUrls: ['./add-organization.component.css']
})
export class AddOrganizationComponent implements OnInit {

     myform: FormGroup;
     passwords: FormGroup;
     organizationName: FormControl;
     organizationAddress: FormControl;
     pinCode: FormControl;
     mobileNumber: FormControl;
     organizationType: string[] = ["WholeSale","Retail"];
     businessType: FormControl;
     ownerName: FormControl;
     password: FormControl;
     rePassword: FormControl;
     telephoneNumber: FormControl;
     gstin: FormControl;




  createFormControls() {

     this.organizationName = new FormControl("", Validators.required);
     this.ownerName = new FormControl("", Validators.required);
     this.organizationAddress = new FormControl("", Validators.required);
     this.pinCode = new FormControl("", Validators.required);
     this.mobileNumber = new FormControl("", Validators.required);
     this.telephoneNumber = new FormControl();
     this.businessType = new FormControl("", Validators.required);
     this.gstin = new FormControl("", [Validators.required]);
     this.passwords = new FormGroup({
      password: this.password = new FormControl("",[Validators.required,Validators.minLength(8)]),
      repassword: this.rePassword = new FormControl("",[Validators.required])
      },{ validators: this.passwordValidator }
      )
  }

  passwordValidator(fb: FormGroup) {
    let password  = fb.controls.password.value;
    let repass = fb.controls.repassword.value; 
      if (repass !== password) {
        return {
          passwordMatch: {
            passwordMatch: password
          }
        }
      }
    return null;
  } 

  createForm() {
    this.myform = new FormGroup({
    ownerName: this.ownerName,
    organizationName: this.organizationName,
    organizationAddress: this.organizationAddress,
    pinCode: this.pinCode,
    mobileNumber: this.mobileNumber,
    telephoneNumber: this.telephoneNumber,
    businessType: this.businessType,
    gstin: this.gstin,
    }); 
  }

  onSubmit() {
    if (this.myform.valid) {
      console.log("Form Submitted!");
      console.log(this.myform.value);
      this.myform.reset();
    }
  }


  constructor() { }

  ngOnInit() {
    this.createFormControls();
    this.createForm();
  }

}

添加组织.html

    <mat-form-field>
            <input matInput placeholder="Set password" type = "password" formControlName="password">
            <mat-error *ngIf="password.errors?.required">Password is required</mat-error>
            <mat-error *ngIf="password.errors?.minlength">
                Password must be {{password.errors.minlength.requiredLength}} characters long, we need another {{password.errors.minlength.requiredLength - password.errors.minlength.actualLength}} characters
            </mat-error>
          </mat-form-field>
          <mat-form-field>
            <input  matInput placeholder="Re-Enter password" type = "password" formControlName="rePassword">
            <mat-error *ngIf="rePassword.errors?.required">Password is required</mat-error>
            <mat-error *ngIf="passwords.validators.passwordValidator">not match</mat-error>
   </mat-form-field>

如果密码和 rePassword 不一样,我希望输出显示错误消息,否则不

4

3 回答 3

0

你已经创造了Form两次。一次,所有控件都在createFormControls(). 另一个在createForm(). createForm 没有该password字段,因此它不会创建密码控件,因​​此 html 在创建带有 name 的 formControl 时会引发错误password

您需要在不改变表单结构的createForm()情况patchValue()下更新控件的值。

于 2019-07-30T18:53:53.933 回答
0

它只是和建议。创建formGroup时可以直接使用

this.myform = new FormGroup({
    ownerName: new FormControl('',Validators.Required),
    organizationName: new FormControl('',Validators.Required),
    organizationAddress: new FormControl('',Validators.Required),
    pinCode: new FormControl('',Validators.Required),
    mobileNumber: new FormControl('',Validators.Required),
    telephoneNumber: new FormControl(''),
    businessType: new FormControl('',Validators.Required),
    gstin: new FormControl('',Validators.Required),
    password:new FormGroup({
      password: new FormControl("",[Validators.required,Validators.minLength(8)]),
      repassword: this.rePassword = new FormControl("",[Validators.required])
      },{ validators: this.passwordValidator }
      )
    }); 

如果您使用 {validators:this.passwordValidator} 您的函数被声明为

passwordValidator(formGroup:FromGroup)
{ 
     ....
}

如果你使用 { validators: this.passwordValidator() } //<--见 ()

passwordValidator()
{
  return (formGroup:FromGroup)=>
  { 
     ....
  }
}
于 2019-07-30T19:14:59.390 回答
0

我就是这样做的:

function passwordValidator() { // wrapper fn
  return (ctrl: AbstractControl) => { // return the function w abstractControl
    const fb = ctrl as FormGroup; //type it
    const passwordCtrl  = fb.get('password'); // get the ctrls
    const repassCtrl = fb.get('repassword'); 
    if (!passwordCtrl || !repassCtrl) // handle errors!
      throw new Error('need password and repass controls');
    if (passwordCtrl.value // make sure forms have values (required error should show in this case, not pass match)
          && repassCtrl.value 
          && repassCtrl.value !== passwordCtrl.value) {
      return {
        passwordMatch: password //redunant
      }
    }
    return null;
  }
}

在你的组件之外定义它(所以它是可重用的)然后使用:

{ validators: [passwordValidator()] }

但是,当您建立组时,您的错误确实在这里:

this.passwords = new FormGroup({
  password: new FormControl("",[Validators.required,Validators.minLength(8)]),
  repassword: new FormControl("",[Validators.required])
  },{ validators: [passwordValidator()] }
)

您只是无法按照您的方式构建表单。你应该像这样定义你的密码和重新密码变量:

get password() { return this.passwords.get('password');
get repassword() { return this.passwords.get('repassword');

要让错误显示为您所拥有的(在 mat 错误字段内),那么您需要将这个奇怪的东西添加到您的组件中:

parentErrorMatcher = {
  isErrorState(control: FormControl): boolean {
    return control.parent.invalid && control.touched;
  }
};

然后在要显示错误的表单字段的模板中使用它:

<mat-form-field [errorStateMatcher]="parentErrorMatcher">

这将导致错误状态反映父级而不是子级

于 2019-07-30T18:16:39.437 回答