Skip to main content
added 1 character in body
Source Link
user12217822
  • 302
  • 1
  • 6
  • 19

I'm using angular 8 and I wrote a dynamic form. I want to write a validator mapper form to validate my form controles but it doesn't work correctly.

ts

       for (const prop of this.formConfig) {
    
          this.form.addControl(
            prop.id, new FormControl(
              prop.value,
            this.mapValidator(prop.validationvalidations)
            )
          )
          }


  mapValidator(validators) {
    if (validators) {
         validators.forEach(validator=>{
      if (validator.type=='required') {

        return [Validators.required];
      }})
    } else {
      return [];
    }
  }

config

"formConfig": [
  {
    "name": "firstName",
    "label": "name ",
     "value" : "",
     "id" : "1"
    "type": "text",
    "validations": [
      {
        "type": "required"
      }
    ]
  }

I'm using angular 8 and I wrote a dynamic form. I want to write a validator mapper form to validate my form controles but it doesn't work correctly.

ts

       for (const prop of this.formConfig) {
    
          this.form.addControl(
            prop.id, new FormControl(
              prop.value,
            this.mapValidator(prop.validation)
            )
          )
          }


  mapValidator(validators) {
    if (validators) {
         validators.forEach(validator=>{
      if (validator.type=='required') {

        return [Validators.required];
      }})
    } else {
      return [];
    }
  }

config

"formConfig": [
  {
    "name": "firstName",
    "label": "name ",
     "value" : "",
     "id" : "1"
    "type": "text",
    "validations": [
      {
        "type": "required"
      }
    ]
  }

I'm using angular 8 and I wrote a dynamic form. I want to write a validator mapper form to validate my form controles but it doesn't work correctly.

ts

       for (const prop of this.formConfig) {
    
          this.form.addControl(
            prop.id, new FormControl(
              prop.value,
            this.mapValidator(prop.validations)
            )
          )
          }


  mapValidator(validators) {
    if (validators) {
         validators.forEach(validator=>{
      if (validator.type=='required') {

        return [Validators.required];
      }})
    } else {
      return [];
    }
  }

config

"formConfig": [
  {
    "name": "firstName",
    "label": "name ",
     "value" : "",
     "id" : "1"
    "type": "text",
    "validations": [
      {
        "type": "required"
      }
    ]
  }
Source Link
user12217822
  • 302
  • 1
  • 6
  • 19

How to write a validator for dynamic form in angular?

I'm using angular 8 and I wrote a dynamic form. I want to write a validator mapper form to validate my form controles but it doesn't work correctly.

ts

       for (const prop of this.formConfig) {
    
          this.form.addControl(
            prop.id, new FormControl(
              prop.value,
            this.mapValidator(prop.validation)
            )
          )
          }


  mapValidator(validators) {
    if (validators) {
         validators.forEach(validator=>{
      if (validator.type=='required') {

        return [Validators.required];
      }})
    } else {
      return [];
    }
  }

config

"formConfig": [
  {
    "name": "firstName",
    "label": "name ",
     "value" : "",
     "id" : "1"
    "type": "text",
    "validations": [
      {
        "type": "required"
      }
    ]
  }