0
votes

I'm using Angular 7 with Dynamic Forms

I store some form configs such as input: "text" etc to construct my forms. These are stored on a database. I store the validation as validator: "Validators.required". Now when I want to construct my dynamic forms and assign the validation to a form control using:

validations.forEach(valid => {
   validList.push(valid.validator);
});
return Validators.compose(validList);

I get the following error: Screenshot

Now I'm assuming this is because I have Validators.required stored as a string which causes it to fail since it is not actually of type Validator which I've imported from angular/forms.

To my question: How can I convert the stored string to the Validators type. I've tried using castValidator = (valid.validator as Validators); which gave me the same error.

I've thought about just doing a switch statement which checks the string then pushes the relevant Validators into the array, but that seems like a poor solution. I would have to account for every Validator type, not even thinking about custom validation.

1

1 Answers

0
votes

If data is saved in the form of string and data is in the form of key value pair. Then you will have to parse what you get from the database using JSON.parse().