Reputation: 7537
I have a Angular reactive forms and i want show validation errors in a material dialog.
There is a way for subscribe formControl.errors
and on error do something?
EG.:
this.formControl.errors.subscribe(errors => {
this.dialog.open(DialogAlertComponent, {data: errors});
});
Upvotes: 1
Views: 1823
Reputation: 363
To show errors when there is a statusChange or valueChange in your form, you can make use of the below 2 observables on your formGroup object.
form: FormGroup;
constructor(private formBuilder: FormBuilder) {}
this.form = this.formBuilder.group({
username: ['', [ Validators.required ]],
password: ['', [ Validators.required ]]
});
To monitor a single formcontrol,
this.form.get('username').valueChanges.subscribe(
result => {
// call your DialogAlertComponent to show errors if any
}
);
To monitor the entire form,
this.form.valueChanges.subscribe(
result => {
// call your DialogAlertComponent to show errors if any
}
);
Here statusChanges Observable can also be used.
Upvotes: 2