guillotinaweb / ngx-schema-form

HTML form generation based on JSON Schema
MIT License
485 stars 173 forks source link

Form fails to render from the response of http call.. #49

Open bhattaca72 opened 7 years ago

bhattaca72 commented 7 years ago

Form renders fine when read from a file. However fails when tried with the response of the http call..

// Observable Version
import { Component, OnInit } from '@angular/core';
import { WidgetRegistry } from "angular2-schema-form";
import { TinyMCEWidget } from "ng2sf-tinymce";
import { Http, Response } from '@angular/http';

@Component({
  selector: 'test-list',
  template: '<schema-form class="col-md-6" [schema]="schemafromhttp" [model]="model" [actions]="actions"></schema-form>',

})
export class TestListComponent {

  errorMessage: string;
  thisSchemaForm: any;
  schemafromfile: any;
  schemafromhttp: any = {'properties': {}, "type": "object"};
  private actions:any = {};
  private model:any = {};

  constructor(private http: Http) {}

  ngOnInit() {
    this.schemafromfile = require("./sampleschema.json");
    console.log("read from file: ", this.schemafromfile);
    console.log("read from file: ", JSON.stringify(this.schemafromfile));
    this.http.get('http://127.0.0.1:8080/getRegSchemaDefinition?type=registration4').subscribe(res => {
      let schemafromhttp1 = res.json();
      schemafromhttp1.fieldsets = [{"fields": ["userID", "appID"]}];
      console.log('read from service: ', schemafromhttp1);
      this.schemafromhttp = schemafromhttp1;
      console.log('read from service: ', JSON.stringify(this.schemafromhttp));
    });
  }
}

the file that renders fine is attached is as, the http response is verified to be same.

{"properties":{"userID":{"type":"string","minLength":7,"maxLength":10,"description":"User ID"},"appID":{"type":"string","minLength":3,"maxLength":3,"description":"Application Identifier"}},"required":["userID"]}

Any heads up if anything specific is missing out?

levequej commented 7 years ago

exact same problem here

levequej commented 7 years ago

FYI I worked around it with adding *ngIf="schema !== undefined" in the HTML, and private schema:any; in the ts code example:

  private schema:any;
<sf-form [schema]="schema" [model]="model" [validators]="fieldValidators" [actions]="actions" *ngIf="schema !== undefined"></sf-form>
schmuli commented 7 years ago

The correct issue here is that the FormComponent's ngOnChanges will throw an error if it is called and this.schema is null or undefined, which will happen if the schema is unavailable when the component is first initialized.

This is the line with the issue: https://github.com/makinacorpus/angular2-schema-form/blob/master/src/form.component.ts#L76

schmuli commented 7 years ago

The same issue is already mentioned by #42 .