2017-09-25 20 views
0

私はAngular 4.Xプロジェクトで作業しており、ボタンをクリックするとHTML入力フィールド(ほとんどがテキスト)が作成されています。動的に入力ボックスの作成はうまくいきますが、これらのフィールドの検証を実装することはできません。次のエラーが表示されます。角度2動的フィールドのフォーム検証

はヌル(...)私は同じのためにplunkを作成している

のプロパティ「無効」読み取ることができません。私が作成していることをplunkのリンクされて、次の - 任意の助けをいただければ幸いです

//Root app component 
import {Component, NgModule} from '@angular/core' 
import {BrowserModule} from '@angular/platform-browser' 
import { FormBuilder, FormGroup, Validators, FormArray, FormControl } from '@angular/forms'; 
import { FormsModule, ReactiveFormsModule } from '@angular/forms'; 

@Component({ 
    selector: 'my-app', 
    template: `<hr> 
       <div> 
       <form [formGroup]="orderForm" (ngSubmit)="OnSubmit(orderForm.value)"> 
        <div> 
        <input type="text" formControlName="customerName"/> 
        <input type="text" formControlName="email"/> 
        </div> 
        <div formArrayName="items" *ngFor="let item of items.controls; let i = index;"> 
        <div [formGroupName]="i"> 
         <input type="text" formControlName="name" placeholder="Item name"/> 
         <small *ngIf="IsValidField('name')" class="text-danger"> 
         Name is required 
         </small> 
         <input type="text" formControlName="description" placeholder="Item description"/> 
         <small *ngIf="IsValidField('description')" class="text-danger"> 
         Description is required 
         </small> 
         <input type="text" formControlName="price" placeholder="Item price"/> 
         <small *ngIf="IsValidField('price')" class="text-danger"> 
         Price is required 
         </small> 
        </div> 
        Chosen name: {{ orderForm.controls.items.controls[i].controls.name.value }} 
        </div> 
        <button type="submit">Save</button> 
        <button type="button" (click)="addItem()">Add More</button> 
       </form> 
       <div>`, 
}) 

export class App { 

    constructor(private formBuilder: FormBuilder) { } 

    public orderForm: FormGroup; 



    ngOnInit() { 
     this.orderForm = this.formBuilder.group({ 
     customerName: '', 
     email: '', 
     items: this.formBuilder.array([ this.createItem()]) 
     }); 
    } 

createItem(): FormGroup { 
    return this.formBuilder.group({ 
     name: ['',[Validators.required,Validators.maxLength(10)]], 
     description: '', 
     price: ['',[Validators.required,Validators.pattern("[(0-9)*]")]] 
    }); 
    } 

    get items(): FormArray { 
    return this.orderForm.get('items') as FormArray; 
    }; 

    addItem(): void { 
    this.items.push(this.createItem()); 
    } 

    public OnSubmit(formValue: any) { 
     console.log(JSON.stringify(formValue)); 
    } 

    public IsValidField(field: string) { 
     return (this.orderForm.get(field).invalid && this.orderForm.get(field).touched) || (this.orderForm.get(field).invalid && this.orderForm); 
    } 

} 

@NgModule({ 
    imports: [ BrowserModule, FormsModule, ReactiveFormsModule ], 
    declarations: [ App ], 
    bootstrap: [ App ] 
}) 
export class AppModule {} 

-

https://plnkr.co/edit/PCFD43GK91zo2ivQ9lf7?p=preview が簡単に参照できるようにすると、以下のコードを見つけてください。

答えて

0

orderFormに動的に追加されたフォームフィールドを検索していますが、これは利用できません。その値にアクセスする前に、フォームに正しく照会する必要があります。私は、動的に作成されたフォームアーキテクチャがどのように理解されるかを理解するのに役立つ図の下に示します。

orderForm (FormGroup) 
    | 
    - Items (FormArray) 
    | 
    - 0 (FormGroup) 
     - name 
     - description 
     - price 
    | 
    - 1 (FormGroup) 
     - name 
     - description 
     - price 
    | 
    |......... 

のでIsValidField機能はindexformArrayの要素、およびフィールド名を取得する必要があります。フォーム要素を簡単に照会することができます。

public IsValidField(i: number, field: any) { 
    var f = this.orderForm 
      .get('items') //retrieve items FormArray 
      .get(i.toString()) //retrieve items FormGroup 
      .get(field); //retrieve items form field 
    return (f.invalid && f.touched) || (f.invalid && this.orderForm); 
} 

それに応じて*ngIfIsValidField関数呼び出しを変更します。

*ngIf="IsValidField(i, 'name')" 
*ngIf="IsValidField(i, 'description')" 
*ngIf="IsValidField(i, 'price')" 

Demo Plunker

+0

おかげパンカジそれは本当に助けました。これらのフィールドにどのようにデータを入力すればいいのか教えてください。私はそれをしようとしていますが、それは機能していません。ここにその[リンク](https://plnkr.co/edit/PCFD43GK91zo2ivQ9lf7?p=preview)のためのPlunkerがあります –

+0

@AmitAnand値を埋めるために '.patchValue'を使用してください –

+0

しかし、私は複数のフィールドのためにそれをやりますあまりにも動的に。あなたはPlunkerを見て、私に解決策を教えてください。 –

関連する問題