Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component } from '@angular/core';
  2. import { FormGroup, FormBuilder, Validators, FormControl } from '@angular/forms';
  3.  
  4. @Component({
  5.   selector: 'app-root',
  6.   templateUrl: './app.component.html',
  7.   styleUrls: ['./app.component.css']
  8. })
  9. export class AppComponent {
  10.   public myForm: FormGroup;
  11.   serverResponse: Object[]
  12.   isFormLoaded: boolean = false;
  13.   constructor(private formBuilder: FormBuilder) {
  14.  
  15.  
  16.   }
  17.   ngOnInit(): void {
  18.     setTimeout(() => {
  19.       this.serverResponse = [
  20.         {
  21.           "id": "3792cedb-a1ac-1c43-b6bf-d439c6175072",
  22.           "attributeName": "First name",
  23.           "required": true,
  24.           "requiredByDefault": true,
  25.           "fieldLength": 30,
  26.           "fieldType": "text",
  27.           "fieldOrder": 0,
  28.           "staticFieldIndex": 1,
  29.           "exampleKey": null,
  30.           "hintKey": null,
  31.           "isRepeatable": null
  32.         },
  33.         {
  34.           "id": "6e150ae3-1458-7944-8cec-5086762c3704",
  35.           "attributeName": "Middle name",
  36.           "required": false,
  37.           "requiredByDefault": true,
  38.           "fieldLength": 30,
  39.           "fieldType": "text",
  40.           "fieldOrder": 1,
  41.           "staticFieldIndex": 2,
  42.           "exampleKey": null,
  43.           "hintKey": null,
  44.           "isRepeatable": null
  45.         },
  46.         {
  47.           "id": "6e150ae3-1458-7944-8cec-5086762c3704",
  48.           "attributeName": "Last name",
  49.           "required": true,
  50.           "requiredByDefault": true,
  51.           "fieldLength": 30,
  52.           "fieldType": "text",
  53.           "fieldOrder": 1,
  54.           "staticFieldIndex": 2,
  55.           "exampleKey": null,
  56.           "hintKey": null,
  57.           "isRepeatable": null
  58.         },
  59.         {
  60.           "id": "6e150ae3-1458-7944-8cec-5086762c3704",
  61.           "attributeName": "nickname",
  62.           "required": true,
  63.           "requiredByDefault": true,
  64.           "fieldLength": 30,
  65.           "fieldType": "text",
  66.           "fieldOrder": 1,
  67.           "staticFieldIndex": 2,
  68.           "exampleKey": null,
  69.           "hintKey": null,
  70.           "isRepeatable": null
  71.         }
  72.       ];
  73.  
  74.  
  75.     }, 1000)
  76.  
  77.     this.myForm = this.formBuilder.group({
  78.     });
  79.   }
  80.   loaded() {
  81.     if (this.serverResponse) {
  82.       this.serverResponse.forEach((control) => {
  83.         if (control['required']) {
  84.           this.myForm.addControl(`${control['attributeName']}`, new FormControl('', Validators.required));
  85.         } else {
  86.           this.myForm.addControl(`${control['attributeName']}`, new FormControl(''));
  87.         }
  88.       })
  89.     }
  90.     this.isFormLoaded = true
  91.   }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement