1
1
import { Injectable } from '@angular/core' ;
2
2
import { FormGroup , FormArray , FormControl , AbstractControl , Validators } from '@angular/forms' ;
3
3
import { FormlyConfig , FieldValidatorFn } from './formly.config' ;
4
- import { FORMLY_VALIDATORS , evalStringExpression , evalExpressionValueSetter , getFieldId , assignModelValue , getValueForKey , isObject , isNullOrUndefined } from './../utils' ;
4
+ import { FORMLY_VALIDATORS , evalStringExpression , evalExpressionValueSetter , getFieldId , assignModelValue , getValueForKey , isObject , isNullOrUndefined , clone } from './../utils' ;
5
5
import { FormlyFieldConfig , FormlyFormOptions } from '../components/formly.field.config' ;
6
6
import { getKeyPath , isUndefined , isFunction } from '../utils' ;
7
7
import { FormlyFormExpression } from './formly.form.expression' ;
@@ -60,6 +60,13 @@ export class FormlyFormBuilder {
60
60
// is last item
61
61
if ( index === paths . length - 1 ) {
62
62
this . addFormControl ( rootForm , field , rootModel , formPath ) ;
63
+ if ( field . fieldArray ) {
64
+ field . fieldGroup = [ ] ;
65
+ ( rootModel [ formPath ] || [ ] ) . forEach ( ( m : any , i : number ) => field . fieldGroup . push (
66
+ { ...clone ( field . fieldArray ) , key : `${ i } ` } ,
67
+ ) ) ;
68
+ }
69
+
63
70
} else {
64
71
let nestedForm = rootForm . get ( formPath ) as FormGroup ;
65
72
if ( ! nestedForm ) {
@@ -232,6 +239,9 @@ export class FormlyFormBuilder {
232
239
233
240
private addFormControl ( form : FormGroup | FormArray , field : FormlyFieldConfig , model : any , path : string ) {
234
241
let control : AbstractControl ;
242
+ const validators = field . validators ? field . validators . validation : undefined ,
243
+ asyncValidators = field . asyncValidators ? field . asyncValidators . validation : undefined ;
244
+
235
245
if ( field . formControl instanceof AbstractControl || form . get ( path ) ) {
236
246
control = field . formControl || form . get ( path ) ;
237
247
if (
@@ -244,23 +254,11 @@ export class FormlyFormBuilder {
244
254
} else if ( field . component && field . component . createControl ) {
245
255
control = field . component . createControl ( model [ path ] , field ) ;
246
256
} else if ( field . fieldGroup && field . key && field . key === path && ! field . fieldArray ) {
247
- control = new FormGroup (
248
- model [ path ] ,
249
- field . validators ? field . validators . validation : undefined ,
250
- field . asyncValidators ? field . asyncValidators . validation : undefined ,
251
- ) ;
257
+ control = new FormGroup ( model [ path ] , validators , asyncValidators ) ;
252
258
} else if ( field . fieldArray && field . key && field . key === path ) {
253
- control = new FormArray (
254
- [ ] ,
255
- field . validators ? field . validators . validation : undefined ,
256
- field . asyncValidators ? field . asyncValidators . validation : undefined ,
257
- ) ;
259
+ control = new FormArray ( [ ] , validators , asyncValidators ) ;
258
260
} else {
259
- control = new FormControl (
260
- model [ path ] ,
261
- field . validators ? field . validators . validation : undefined ,
262
- field . asyncValidators ? field . asyncValidators . validation : undefined ,
263
- ) ;
261
+ control = new FormControl ( model [ path ] , validators , asyncValidators ) ;
264
262
}
265
263
266
264
if ( field . templateOptions . disabled ) {
0 commit comments