1

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});
});
2

1 Answer 1

2

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.

  • valueChanges: Observable --> emits an event every time the value of the control changes
  • statusChanges: Observable ---> emits an event every time the validation status of the control recalculates.
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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.