5

I'm struggling with creating dynamically forms from JSON Schema in Angular 8. I found some libraries for that, but one is very old (last commit ~2 yr ago) and fork of it from this year.

I don't want to use first one, because it's deprecated with angular 8, so my choise was the second one. I have trouble because this project isn't on any npm repositories, only on github. I downloaded it form git like this:

npm i rhalff/angular2-json-schema-form#angular-8

When I started to build my project with this dependency and after attach the import into app.module.ts the result is like this:

ERROR in src/app/app.module.ts:20:65 - error TS2307: Cannot find module 'angular2-json-schema-form'.

I found that in node_module there isn't any src or dist folder and then I looked into github projecet and I found that in /.npmignore there is a src.

I have a some solution about that, but it's not the final one and I think is really bad idea. I will locally clone git repository, next then build it with npm run build and then npm link and then npm link <someName> in project catalog.

Is there any solution of this problem? Or any other libraries for converting JSON Schema for Angular 8 forms?

4
  • If you think you can make it work fork the package in your github then while installing npm run this npm install --save username/repo#branch-name-or-commit-or-tag from your github account (make sure it is public for npm to find)after making your desired changes. I mean if you are too desperate to use this. Looks like the author is silent from 2yrs Commented Sep 16, 2019 at 11:24
  • I point it out that the first one solution isn't my choise, and in this fork (second one solution) there is a branch called angular8 and I want to use it from it. Commented Sep 16, 2019 at 11:27
  • ok sorry i didn't see that. yup you can fork it again from here and try removing the src from npm ignore see if it works in your own github account Commented Sep 16, 2019 at 11:30
  • I found working fork which is continuously support and I have integrate it with success into my project. If someone would be looking for working example, here it is this fork on github: github.com/hamzahamidi/Angular6-json-schema-form and on npm: npmjs.com/package/angular6-json-schema-form. Commented Sep 17, 2019 at 14:09

1 Answer 1

0

You can do something like this. It may not cover some complex scenarios but can do good with less complex forms.

in form.ts

public form: FormGroup = new FormGroup({});
public fields = [
  {label: 'Full Name', name: 'full_name', type: 'text'},
  {label: 'Email', name: 'email', type: 'email'},
  {label: 'Comments', name: 'comments', type: 'textarea'},
]

constructForm(){
    let controls = {};
    fields.map(field => {
       controls[field.name] = new FormControl('');
    });
    this.form = new FormGroup(controls);
  }

in form.html

   <form [formGroup]="form">
     <div *ngFor="let field of fields">
       <label for="{{field.name}}">{{field.label}}</label>
       <div [ngSwitch]="field.type">
    
          <!-- textarea -->
          <textarea *ngSwitchCase="'textarea'" [formControlName]="field.name"></textarea>
    
          <!-- other input fields (text, password, number, email) -->
          <input *ngSwitchDefault id="{{field.id}}" type="{{field.type || 'text'}}" [formControlName]="field.name" />
        </div>
     </div>
   </form>

This is just a basic structure of how I am doing it. You may add more props to field objects even an "on change" handler function and play more. i.e. css_classes, maxlength, and can do in html like field.propName

like:

{label: 'Complex Field', name: 'complex_field, type: 'text', onChange: () => {console.log("Something Happened")}}

And

<input type="{{field.type}} (change)="field.onChange()" [formControlName]="field.name" />
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.