I tried to reduce script in reactive form validation but not working. Anyone can find the mistake from my code.
Form Angular Builder Code
public obj=["firstName","lastName",'password','confirmPassword'];
ngOnInit() {
this.registerForm = this.formBuilder.group({
for(var i=0;i<=this.obj.length;i++){
this.obj[i]: ['', Validators.required]
}
email: new FormControl('',{Validators:[Validators.required],updateOn:'blur'})
} );
}
https://stackblitz.com/edit/angular-7-reactive-form-validation-rzsfa9?file=app/app.component.ts
Please change the declaration and ngOnInit() function to below code
registerForm;
submitted = false;
obj=["firstName","lastName",'email','password','confirmPassword'];
constructor(private formBuilder: FormBuilder) { }
ngOnInit() {
this.registerForm = new FormGroup({});
for(var i=0;i<=this.obj.length;i++){
if(this.obj[i] == "email"){
this.registerForm.addControl(this.obj[i], new FormControl('',[ Validators.required,Validators.email]))
}
else
{
this.registerForm.addControl(this.obj[i], new FormControl('', Validators.required))
}
};
}
Here is the link to the updated code
https://stackblitz.com/edit/angular-7-reactive-form-validation-dmq3bq