1

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"
      }
    ]
  }
2
  • Looks good, besides this: this.mapValidator(prop.validation). This should be this.mapValidator(prop.validations) I guess?
    – MikeOne
    Commented Aug 17, 2020 at 16:56
  • @MikeOne Yes, You are right Commented Aug 17, 2020 at 16:59

0

Your Answer

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

Browse other questions tagged or ask your own question.