如何使用反应式表单验证来验证文本框

时间:2019-03-06 11:00:42

标签: angular5 angular6 angular7

我已经为文本框创建了自定义文本组件,并且我试图通过反应式表单验证进行验证,但是无法正常工作。

获取错误:

错误 错误:formControlName必须与父formGroup指令一起使用。您将要添加一个formGroup 指令并将其传递给现有的FormGroup实例(您可以在类中创建一个)。

任何人都可以帮助解决此问题吗?

https://stackblitz.com/edit/angular-6-reactive-form-validation-9pu6hq?file=app%2Fapp.component.html

app.component.html:

<app-textbox formControlName="password"></app-textbox>

app.component.ts:

this.registerForm = this.formBuilder.group({
        firstName: ['', Validators.required],
        password: ['', [Validators.required, Validators.minLength(6)]]
    });

1 个答案:

答案 0 :(得分:0)

您需要将app-textbox放在分配给表单组的form元素之间:

   <form [group]="registerForm">
      <app-textbox formControlName="password"></app-textbox>
   </form>