1

I have a custom control using ControlValueAccessor, and my control is in a form with a bunch of material input controls.

The material inputs show the red validation underline in 3 cases.

  1. the control has been touched and is invalid
  2. the control is dirty and invalid
  3. the form containing the control has been submitted and the control is invalid

I want to mimic this behavior in my custom control. I've used the injector to get a reference to the NgControl which lets me determine if the control is touched/dirty and invalid. But I can't figure out how to access the parent NgForm in a generic way to determine if it is submitted.

I don't want to explicitly pass it into my component, and I shouldn't need to since it isn't being explicitly passed to material input either yet that control is able to determine if the form has been submitted.

How do I get a hold of the parent forms submitted state?

1 Answer 1

2

I ended up finding it in the injector, had to grab the FormGroupDirective

const formgroup: FormGroupDirective = this.injector.get(FormGroupDirective, null);

The directive has a submitted property I was able to use

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

1 Comment

I'm surprised that the article from angular material about implementing a custom form control didn't take care of the form submission. It's not fully usable otherwise.. and that's my implementation of a country autocomplete with your solution: stackblitz.com/edit/angular-fxu8rz-2xjnod?file=src/app/…

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.