简体   繁体   English

angular2 - 验证父FormGroup的子组件中的FormControlName

[英]angular2 - validating FormControlName in child component of parent FormGroup

I have an angular component corresponding a form/page that is generating an indeterminate amount of child components, each representing an individual field, and I would like the parent component's FormGroup to validate the fields contained in the child components. 我有一个角度组件对应一个表单/页面生成一个不确定数量的子组件,每个组件代表一个单独的字段,我希望父组件的FormGroup验证子组件中包含的字段。 Only when I do so, I get an error: 只有当我这样做时,我才会收到错误:

A FormControlName must have a corresponding FormGroup. FormControlName必须具有相应的FormGroup。

Here is the template code for my parent component: 这是我父组件的模板代码:

<div class="form-group" [formGroup]="parentGroup">
  <table>
    <tbody>
      <tr *ngFor="let i of array">
        <child [property]="i" [options]="myForm.controls[i.id]"></child>
      </tr>
    </tbody>
  </table>
</div>

The form is defined in the component file here. 表单在此处的组件文件中定义。 I'm adding the FormControls according to how many child components we're adding: 我根据我们添加的子组件数量添加FormControls:

private formAttrs: FormGroup;

constructor(private _fb: FormBuilder) { }

ngOnInit() {
  this.myForm = this._fb.group({});
  for (var i = 0; i < this.array.length; i++) {
    this.formAttrs.addControl(this.array[i].id, new FormControl(this.array[i].value, Validators.required));
  }
}

The template code for the child component is this: 子组件的模板代码如下:

<td class="prompt">
  {{i.label}}
</td>
<td class="required" width="1%">
  <span *ngIf="property.required">*</span>
</td>
<td>
  <input type="text " class="form-control" [ngClass]="{error: !options.valid}" formControlName="property.id">
</td>
<td>

While there is nothing defined in the child component class (other than the "property" and the FormControl element passed down for "options"), I would think that the formGroup in the parent component would be able to match with the formControlName in the child component, but instead I get the error: 虽然子组件类中没有定义任何内容(除了“属性”和为“选项”传递下来的FormControl元素),我认为父组件中的formGroup将能够与子组件中的formControlName匹配组件,但我得到错误:

EXCEPTION: Error in ./ChildComponent class ChildComponent - inline 
template:7:109 caused by: formControlName must be used with a parent 
formGroup directive.  You'll want to add a formGroup directive and pass 
it an existing FormGroup instance (you can create one in your class).

Is there a way I can get around this error? 有没有办法解决这个错误? If not, is there another solution to this problem that someone can suggest? 如果没有,是否有人可以提出这个问题的另一个解决方案?

Thanks in advance. 提前致谢。

There are a couple of things I came across implementing this in a Plunker. 我在Plunker中遇到了一些实现这一点的事情。

First, we'll need to pass in our formGroup from the parent to the child so we have a FormGroup to satisfy the templating engine's enforcement of FormControls being a part of a FormGroup: 首先,我们需要将formGroup从父级传递给子级,因此我们有一个FormGroup来满足模板引擎对FormControls的一部分的执行:

child.component.ts child.component.ts

@Input() parentGroup: FormGroup;

child.component.html child.component.html

<td [formGroup]="parentGroup">
<...>
</td>

Then we'll also need to set the [formControl] or evaluate property.id , otherwise it looks for the name "property.id": 然后我们还需要设置[formControl] 或者评估property.id ,否则它会查找名称“property.id”:

<input type="text " class="form-control" [ngClass]="{error: !options.valid}" [formControl]="options"/>

or 要么

<input type="text " class="form-control" [ngClass]="{error: !options.valid}" formControlName="{{property.id}}"/>

Your code was using different variables binding the formGroup and using formAttrs which was a little unclear as to what was going on so I went ahead and collapsed them to one and you can see that in the Plunker: http://plnkr.co/edit/3MRiO9bGNFAkN2HNN7wg?p=preview 你的代码使用不同的变量来绑定formGroup并使用formAttrs ,这有点不清楚发生了什么,所以我继续把它们折叠成一个你可以在Plunker中看到: http ://plnkr.co/edit / 3MRiO9bGNFAkN2HNN7wg p =预览

The problem here is, that it is not possible to have the same form control name multiple times in one form group. 这里的问题是,在一个表单组中不可能多次使用相同的表单控件名称。

You need to declare an own form group for each child component and then you can iterate over it in the parent component based on your reference attribute. 您需要为每个子组件声明一个自己的表单组,然后您可以根据您的引用属性在父组件中迭代它。 You can get each child form control with the directive component method FormGroupDirective.getControl(controlName) as you can see in documentation: https://angular.io/docs/ts/latest/api/forms/index/FormGroupDirective-directive.html 您可以使用指令组件方法FormGroupDirective.getControl(controlName)获取每个子窗体控件,如文档中所示: httpsFormGroupDirective.getControl(controlName)

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

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