0

I am currently developing a dynamic form builder it has an admin side and a user side, on the admin side you can set the name of the input and type eg: first name and textbox you can add or take away from fields and there are a set amount of types you can choose from, when you are done the admin will then save the form to the Database. On the user frontend the from is pulled from the Database and using patchValue the from is set the problem I am having is with the user's input to the questions on the form. I am trying to figure out how I can first set the type and placeholder of the input that the user uses to answer the questions.

As well as dynamically apply validation rules to each input, so, for instance, a user wants to fill out a form with their first name, last name, age, and CV for the first and last name I want to have required and a regex expression to only allow certain characters, for the age I want to have a min and max number and cv required. and when the user submits the form I only want to send the name and value of an input and not the whole form.

But keep in mind that these forms are completely dynamic so I need to validate the user's input based on the input type that is set

/*Typescript Code*/
import { Component, OnInit } from '@angular/core';
import { FormArray, FormBuilder, FormGroup, Validators } from '@angular/forms';
import { FormService} from '../form.service';
import { ActivatedRoute } from '@angular/router';
@Component({
  selector: 'app-reactive',
  templateUrl: './reactive.component.html',
  styleUrls: ['./reactive.component.scss']
})
export class ReactiveComponent implements OnInit {
  public form: FormGroup;
  public fieldList: any;
  types: Array<any>;
  formData: any;
  Param: string;

  get contactFormGroup() {
    return this.form.get('inputs') as FormArray;
  }
  constructor(
    private route: ActivatedRoute,
    private fb: FormBuilder,
    private api: FormService) { }

  ngOnInit() {
  /*First I initlize the form*/
    this.form = this.fb.group({
      name: [null, Validators.compose([Validators.required])],
      organization: [null, Validators.compose([Validators.required])],
      inputs: this.fb.array([this.createForm()])
    });
    this.route.paramMap.subscribe(params => {
      this.Param = params.get('id');
      this.getForm(this.Param);
    });
    // set fieldslist to this field
    this.fieldList = this.form.get('inputs') as FormArray;
  }

  // formgroup
  /*The I initilize the form array of inputs*/
  createForm(): FormGroup {
    return this.fb.group({
      type: [null, Validators.compose([Validators.required])],/*These come from the DB*/
      name: [null, Validators.compose([Validators.required])],/*These come from the DB*/
      value: [null, Validators.compose([Validators.required])] /*This gets filled in by the user*/
    });
  }
  /*The I use my API service to call the DB using the form Id*/
  getForm(id) {
    this.api.getForm(id).subscribe(
      (data: any) => this.setForm(data)
    );
  }
getFieldsFormGroup(index): FormGroup {
    const formGroup = this.fieldList.controls[index] as FormGroup;
    return formGroup;
  }
  /*Here I set my form data*/
  setForm(data) {
    const d = data.results;
    this.form.patchValue({
      name: [d[0].form_name],
      organization: [d[0].org],
    });
    this.form.setControl('inputs', this.setExistingFields(d[0].fields));
  }
  /*Here I set my inputs array data*/
setExistingFields(fields: any): FormArray {
  const formArray = new FormArray([]);
  this.fieldList = formArray;
  fields.forEach(f => {
    formArray.push(this.fb.group({
      name: f.name,
      type: f.type,
      value: f.value
    }));
  });
  return formArray;
}
submit() {
/* Here when I send my form back to the server I only want to send the inputs name and value nothing else */
  console.log(this.form.value.inputs);
 }
}
/*HTML CODE*/
<div class="container p-1">
  <div class="row">
    <div class="col-12">
      <form class="md-form" [formGroup]="form" #submittion (submit)="submit()">
        <div class="card">
          <div class="card-header">Form Name</div>
          <div class="card-body">
            <div class="row">
              <div class="form-group col-6">
                <i class="fab fa-wpforms prefix"></i>
                <input class="form-control" placeholder="Form Name" formControlName="name" type="text" readonly>

              </div>
              <div class="form-group col-6">
                <i class="far fa-building prefix"></i>
                <input class="form-control" placeholder="Organization" formControlName="organization" type="text"
                  readonly>
              </div>
            </div>
          </div>
          <div class="card-header col-12">Please fill in all fields. </div>
          <div class="card-body" formArrayName="inputs">
            <div class="row">
              <div class="col-6" *ngFor="let contact of contactFormGroup.controls; let i = index;">
                <div [formGroupName]="i" class="row z-depth-1 m-1 p-1">
                  <div class="form-group col-6">
                    <input class="form-control" formControlName="name" type="text" readonly>
                  </div>
                  <div class="form-group col-6">
                  <1-- This is the input that the user will fill in and I am not sure how to dynamically set the type or placeholder this is what I am trying to achieve -->
                    <input class="form-control" type="" placeholder=""
                      formControlName="value" />
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </form>
    </div>
  </div>
</div>
<div class="icon-bar round">
  <button class="btn btn-success round m-1" type="submit" (click)="submit()"><i class="fas fa-save">  Save Form</i></button>
</div>

Any help is very much appricated on this I have been trying to figure it out for a while now, thank you

3
  • it's a bit old, but I hope this help you stackoverflow.com/questions/57601703/… Commented Nov 12, 2019 at 14:54
  • It's the right idea but I am a little puzzled on how I can apply it to my code Commented Nov 12, 2019 at 15:06
  • Maybe if I just have all of the validators in the case I won't have to touch the HTML so that will solve my problem Commented Nov 12, 2019 at 15:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.