Skip to content

Commit

Permalink
fix(core): rely on internal model when model input is empty (#2520)
Browse files Browse the repository at this point in the history
fix #2493
  • Loading branch information
aitboudad committed Sep 26, 2020
1 parent a430b3b commit efe68f9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/core/src/lib/components/formly.form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ export class FormlyForm implements DoCheck, OnChanges, OnDestroy {

@Input()
set model(model: any) { this._model = this.immutable ? clone(model) : model; }
get model() { return this._model || {}; }
get model() {
if (!this._model) {
this._model = {};
}

return this._model;
}

@Input()
set fields(fields: FormlyFieldConfig[]) { this._fields = this.immutable ? clone(fields) : fields; }
Expand Down
18 changes: 17 additions & 1 deletion src/core/src/lib/templates/field-array.type.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ describe('Array Field Type', () => {
subscription.unsubscribe();
});


it('should share formControl when field key is duplicated', () => {
app.fields = [
{
Expand Down Expand Up @@ -283,6 +282,23 @@ describe('Array Field Type', () => {
expect(form.at(1)).not.toEqual(form.at(0));
expect(form.at(1).value).toEqual(null);
});

// https://github.com/ngx-formly/ngx-formly/issues/2493
it('should add field when model is null', () => {
app.model = null;
app.fields = [{
key: 'array',
type: 'array',
}];

const fixture = createFormlyTestComponent();

fixture.nativeElement.querySelector('#add').click();
fixture.detectChanges();

expect(app.fields[0].fieldGroup.length).toEqual(1);
expect(app.form.value).toEqual({ array: [null] });
});
});

@Component({ selector: 'formly-form-test', template: '', entryComponents: [] })
Expand Down

0 comments on commit efe68f9

Please sign in to comment.