2017-12-13 7 views
2

サービスの応答から動的FormGroupFormArrayを作成しています。入力のいずれかに値がある場合はフォーム配列を検証したいと考えていました。あなたはこのようなものを使用でき動的フォームの配列の検証角2

this.billingForm = this._fb.group({ 
    itemRows: this._fb.array([this.initItemRows()]) 
}); 

    initItemRows() { 
    return this._fb.group({ 
     Id: '', 
     orgName: '', 
     billing: this._fb.array([]), 
     payment: this._fb.array([]) 
    }); 
    } 
+1

あなたが検証したいフォームの配列? – Crappy

+0

請求と支払いの両方の配列 – Muthu

答えて

2

// Subscribe to value changes on FormGroup 
this._fb.valueChanges.subscribe(c => { 
    // Check empty values 
    if (this._fb.controls['Id'].value !== '' && this._fb.controls['orgName'].value !== '') { 
     // Set validators 
     this._fb.controls['billing'].validator = Validators.required; // You can specify any validator method here 
     this._fb.controls['payment'].validator = Validators.required; 
    } else { 
     this._fb.controls['billing'].validator = null; 
     this._fb.controls['payment'].validator = null; 
    } 
}); 

をあなたはここでカスタムバリデータの詳細な情報を得ることができます。Click!

+0

私はこれをコンストラクタまたはサブミット関数で使用します – Muthu

+0

これをコンポーネントコンストラクタに配置します – Crappy

+0

しかし、 '_fb'は' FormBuilder'です。 'billingForm.valueChanges'であってはいけませんか? –

関連する問題