question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

add markAsTouched to ControlValueAccessor interface

See original GitHub issue

Which @angular/* package(s) are relevant/releated to the feature request?

forms

Description

When using the ControlValueAccessor in a component you can update the value on the parent form with the onChange callback and you write a value from the parent in the component using the writeValue implementation. Similarly you can update the touched status on the parent with the onTouched callback, but there is no way for the parent to signal to the component that it should update it’s internal touched status…

Consider using ControlValueAccessor in a component to wrap a date field using dropdowns for each individual date part. So a dropdown for day, month and year. Currently it is nog possible to mark these dropdowns as touched, when the markAllAsTouched method on the parent form is called. In order to do this you have to update the component yourself some other way

Proposed solution

add an optional markAsTouched() or writeTouched(value: boolean) method to the ControlValueAccessor interface, that gets called whenever the parent form tries to update the touched status of the formControl.

Alternatives considered

I’ve included the parent FormControl in the component with FormGroupDirective. Like this…

constructor(private rootFormGroup: FormGroupDirective) {
}

ngOnInit() {
  ...
  this.rootControl = this.rootFormGroup.get(this.formControlName);
}

and then just use the touched property of the rootControl in addition to the touched property of the individual child controls

Another alternative is to just declare another Input() touched = false in the component, and update that value manually whenever you call the markAllAsTouched on the parent form.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:29
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
kekel87commented, Oct 7, 2022

Hi, we are also very interested in this feature.

Because we want to display invalid fields when users press the submit button on large CVA forms.

For the moment we are using this not very clean directive, but it does the job.

0reactions
angular-robot[bot]commented, May 9, 2022

Thank you for submitting your feature request! Looks like during the polling process it didn’t collect a sufficient number of votes to move to the next stage.

We want to keep Angular rich and ergonomic and at the same time be mindful about its scope and learning journey. If you think your request could live outside Angular’s scope, we’d encourage you to collaborate with the community on publishing it as an open source package.

You can find more details about the feature request process in our documentation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular ControlValueAccessor and markAsTouched
I'm using angular 9 with Angular Material and I have a custom control by implementing the ControlValueAccessor interface. Everything ...
Read more >
How To Use ControlValueAccessor to Create Custom Form ...
Learn how to implement the ControlValueAccessor interface to seamlessly integrate custom form controls into your forms.
Read more >
Angular Custom Form Controls - Complete Guide
Build a fully functional custom form control, compatible with template-driven and reactive forms, as well as with all built-in and custom ...
Read more >
Angular: Nested Reactive Forms Using ... - Medium
The ControlValueAccessor interface is actually what you use to build value ... This tells Angular to add our custom validators to the existing...
Read more >
ControlValueAccessor - Angular
Descriptionlink. Implement this interface to create a custom form control directive that integrates with Angular forms. Methodslink ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found