dschnelldavis / angular2-json-schema-form

Angular 2 JSON Schema Form builder
MIT License
284 stars 178 forks source link

Setting the form data where there are null objects causes error. #157

Open Thorski opened 6 years ago

Thorski commented 6 years ago

Issue type

I'm submitting a (check one): [ x] Bug report [ ] Feature request [ ] Regression (something that used to work, but stopped working in a newer version) [ ] Support request [ ] Documentation issue or request

Current behavior

If you change the data via javascript to such that objects have null references, an error is thrown: ConfigOrgComponent.html:6 ERROR TypeError: Cannot convert undefined or null to object at Function.keys (<anonymous>) at FormGroup.patchValue (forms.js:4546) at eval (forms.js:4548) at Array.forEach (<anonymous>) at FormGroup.patchValue (forms.js:4546) at JsonSchemaFormComponent.setFormValues (angular2-json-schema-form.es5.js:63705) at JsonSchemaFormComponent.updateForm (angular2-json-schema-form.es5.js:63673) at JsonSchemaFormComponent.ngOnChanges (angular2-json-schema-form.es5.js:63631) at checkAndUpdateDirectiveInline (core.js:12092) at checkAndUpdateNodeInline (core.js:13598)

This data loads fine in the playground, it only seems to occur if you change the data on an existing form, for example: <json-schema-form [form]="detailForm" ... Like this: this.detailForm = _.cloneDeep(settings);

Where data is like this: "data": { "id": "12", "name": "Mike's Consortium", "address": null, "contactName": null, "alternateContactName": null, "phoneNumbers": null }

Expected behavior

This worked fine in previous releases (0.5.0-alpha.8).

So It would be nice if it didn't throw the error and behaved as it does if initially created with this data.

IMPORTANT: How can we reproduce your problem?

See above.

This may be related to: #152

Environment

OS name & version: Windows 10 Browser name & version: Chrome Angular version: 5.0.2 Angular JSON Schema Form version(s): 0.6.0-alpha.7

More... Here is a full example schema/data. It works fine in the playground but not if I set it as shown above.

{ "schema": { "required": [ "name" ], "type": "object", "properties": { "id": { "type": "string", "default": "-1" }, "name": { "type": "string" }, "contactName": { "type": "string" }, "alternateContactName": { "type": "string" }, "address": { "type": "object", "properties": { "line1": { "type": "string" }, "line2": { "type": "string" }, "city": { "type": "string" }, "state": { "type": "string" }, "zipCode": { "type": "string" } } }, "phoneNumbers": { "type": "array", "items": { "type": "object", "properties": { "number": { "type": "string" }, "type": { "type": "string", "enum": [ "cell", "home", "work" ] } }, "required": [ "number", "type" ] } } } }, "layout": [ { "key": "contactName", "notitle": "true", "placeholder": "Contact Name" }, { "key": "alternateContactName", "notitle": "true", "placeholder": "Alternate Contact Name" }, { "key": "address.line1", "notitle": "true", "placeholder": "Street Address" }, { "key": "address.line2", "notitle": "true", "placeholder": "Apt/Suite/Other" }, { "type": "div", "display": "flex", "flex-direction": "row", "fxLayoutGap": "12px", "items": [ { "key": "address.city", "flex": "3 3 150px", "notitle": true, "placeholder": "City" }, { "key": "address.state", "flex": "1 1 50px", "notitle": true, "placeholder": "State" }, { "key": "address.zipCode", "flex": "2 2 100px", "notitle": true, "placeholder": "Zip Code" } ] }, { "key": "phoneNumbers", "type": "array", "flex-direction": "row", "items": [ { "type": "div", "displayFlex": true, "flex-direction": "row", "fxLayoutGap": "12px", "items": [ { "key": "phoneNumbers[].type", "flex": "1 1 100px", "notitle": true, "required": true }, { "key": "phoneNumbers[].number", "flex": "4 4 200px", "notitle": true } ] } ] } ], "data": { "id": "12", "name": "Mike's Consortium", "address": null, "contactName": null, "alternateContactName": null, "phoneNumbers": null } }

roshan-lanewala commented 6 years ago

I have the same issue. I got one property in schema "default": null and this fails.

Were you able to find a fix?

tmburnell commented 6 years ago

This is not a fix but i have a workaround that i posted in #152

I did some research and the big actually looked like it was in angular ... it did not check the existence of an object before running the foreach on it. So i might be remedied in future angular updates. However I never opened a ticket in angular (i would have to refind the code).

akvaliya commented 5 years ago

Any progress in this issue?

GonziHere commented 5 years ago

problem is here:

    FormGroup.prototype.patchValue = function (value, options) {
        var _this = this;
        if (options === void 0) { options = {}; }
        Object.keys(value).forEach(function (name) {
            if (_this.controls[name]) {
                _this.controls[name].patchValue(value[name], { onlySelf: true, emitEvent: options.emitEvent });
            }
        });
        this.updateValueAndValidity(options);
    };

because this function assumes that value is object, not null. There isn't any typecheck before Object.keys(value) is called.