Skip to content

Commit 883a63c

Browse files
committed
feat(core): move createControl FieldArrayType to core
BREAKING CHANGE: createControl in FieldArrayType has been removed fix #909
1 parent ed40290 commit 883a63c

File tree

3 files changed

+67
-64
lines changed

3 files changed

+67
-64
lines changed

src/core/src/services/formly.form.builder.spec.ts

Lines changed: 53 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,39 +23,41 @@ describe('FormlyFormBuilder service', () => {
2323
);
2424
});
2525

26-
it('should use the same formcontrol for fields that use the same key', () => {
27-
const fields: FormlyFieldConfig[] = [
28-
{ key: 'test', type: 'input', hide: true },
29-
{ key: 'test', type: 'input' },
30-
];
31-
32-
builder.buildForm(form, fields, {}, {});
33-
expect(fields[0].formControl).toEqual(fields[1].formControl);
34-
});
26+
describe('formcontrol', () => {
27+
it('should use the same formcontrol for fields that use the same key', () => {
28+
const fields: FormlyFieldConfig[] = [
29+
{ key: 'test', type: 'input', hide: true },
30+
{ key: 'test', type: 'input' },
31+
];
3532

36-
it('should update the formcontrol value for fields that already has formcontrol', () => {
37-
const fields: FormlyFieldConfig[] = [
38-
{
39-
key: 'test',
40-
type: 'input',
41-
formControl: new FormControl(),
42-
},
43-
{
44-
key: 'fieldGroup',
45-
type: 'input',
46-
formControl: new FormGroup({ aa: new FormControl('aa') }),
47-
},
48-
{
49-
key: 'fieldArray',
50-
type: 'input',
51-
formControl: new FormArray([new FormControl('aa')]),
52-
},
53-
];
54-
55-
builder.buildForm(form, fields, { test: 'test' }, {});
56-
expect(fields[0].formControl.value).toEqual('test');
57-
expect(fields[1].formControl.value).toEqual({ aa: 'aa' });
58-
expect(fields[2].formControl.value).toEqual(['aa']);
33+
builder.buildForm(form, fields, {}, {});
34+
expect(fields[0].formControl).toEqual(fields[1].formControl);
35+
});
36+
37+
it('should update the formcontrol value for fields that already has formcontrol', () => {
38+
const fields: FormlyFieldConfig[] = [
39+
{
40+
key: 'test',
41+
type: 'input',
42+
formControl: new FormControl(),
43+
},
44+
{
45+
key: 'fieldGroup',
46+
type: 'input',
47+
formControl: new FormGroup({ aa: new FormControl('aa') }),
48+
},
49+
{
50+
key: 'fieldArray',
51+
type: 'input',
52+
formControl: new FormArray([new FormControl('aa')]),
53+
},
54+
];
55+
56+
builder.buildForm(form, fields, { test: 'test' }, {});
57+
expect(fields[0].formControl.value).toEqual('test');
58+
expect(fields[1].formControl.value).toEqual({ aa: 'aa' });
59+
expect(fields[2].formControl.value).toEqual(['aa']);
60+
});
5961
});
6062

6163
describe('initialise default TemplateOptions', () => {
@@ -441,6 +443,25 @@ describe('FormlyFormBuilder service', () => {
441443
});
442444
});
443445
});
446+
447+
describe('fieldArray', () => {
448+
it('should create/build fieldGroup when model is set', () => {
449+
const fields: FormlyFieldConfig[] = [
450+
{
451+
key: 'array',
452+
type: 'input',
453+
fieldArray: {
454+
key: 'test',
455+
type: 'input',
456+
},
457+
},
458+
];
459+
460+
builder.buildForm(form, fields, { array: ['aaa'] }, {});
461+
462+
expect(fields[0].fieldGroup.length).toEqual(1);
463+
});
464+
});
444465
});
445466

446467
export class TestComponentThatCreatesControl {

src/core/src/services/formly.form.builder.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Injectable } from '@angular/core';
22
import { FormGroup, FormArray, FormControl, AbstractControl, Validators } from '@angular/forms';
33
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';
55
import { FormlyFieldConfig, FormlyFormOptions } from '../components/formly.field.config';
66
import { getKeyPath, isUndefined, isFunction } from '../utils';
77
import { FormlyFormExpression } from './formly.form.expression';
@@ -60,6 +60,13 @@ export class FormlyFormBuilder {
6060
// is last item
6161
if (index === paths.length - 1) {
6262
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+
6370
} else {
6471
let nestedForm = rootForm.get(formPath) as FormGroup;
6572
if (!nestedForm) {
@@ -232,6 +239,9 @@ export class FormlyFormBuilder {
232239

233240
private addFormControl(form: FormGroup | FormArray, field: FormlyFieldConfig, model: any, path: string) {
234241
let control: AbstractControl;
242+
const validators = field.validators ? field.validators.validation : undefined,
243+
asyncValidators = field.asyncValidators ? field.asyncValidators.validation : undefined;
244+
235245
if (field.formControl instanceof AbstractControl || form.get(path)) {
236246
control = field.formControl || form.get(path);
237247
if (
@@ -244,23 +254,11 @@ export class FormlyFormBuilder {
244254
} else if (field.component && field.component.createControl) {
245255
control = field.component.createControl(model[path], field);
246256
} 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);
252258
} 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);
258260
} 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);
264262
}
265263

266264
if (field.templateOptions.disabled) {

src/core/src/templates/field-array.type.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,11 @@
11
import { FormArray } from '@angular/forms';
2-
import { FormlyFieldConfig } from '../components/formly.field.config';
32
import { FieldType } from './field.type';
43
import { clone, isNullOrUndefined } from '../utils';
54
import { FormlyFormBuilder } from '../services/formly.form.builder';
65

76
export abstract class FieldArrayType extends FieldType {
87
formControl: FormArray;
98

10-
static createControl(model: any, field: FormlyFieldConfig): FormArray {
11-
const form = new FormArray(
12-
[],
13-
field.validators ? field.validators.validation : undefined,
14-
field.asyncValidators ? field.asyncValidators.validation : undefined,
15-
);
16-
17-
field.fieldGroup = [];
18-
(model || []).forEach((m: any, i: number) => field.fieldGroup.push(
19-
{ ...clone(field.fieldArray), key: `${i}` },
20-
));
21-
22-
return form;
23-
}
24-
259
constructor(private builder: FormlyFormBuilder) {
2610
super();
2711
}

0 commit comments

Comments
 (0)