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.

CustomValueAccessor component is not updating mat-form-field with invalid state

See original GitHub issue

Bug, feature request, or proposal:

Bug? Ease of use?

What is the expected behavior?

I would like to be able to write a simple component that wraps a mat-form-field/mat-input so that it can be reused across many apps within my enterprise. Example is in the Stackblitz below, basically I want a form field that contains a password input with a suffix of an icon where you can toggle between password/text.

I want to use this within a reactive form, and have the validation “just work”.

Ideally, I would not have to implement ControlValueAccessor on my custom component - it’s not really a custom input, it’s just wrapping one so I can easily reuse it.

What is the current behavior?

There are 2 issues currently. First, I would like to not have to implement ControlValueAccessor. I tried to do this by including formControlName=“password” inside my component’s template (in the real world I’d like to be able to bind to [formControlName]=“someInputField” ), but this yields an error: Error: formControlName must be used with a parent formGroup directive. This is evident if you edit the app.component.html in the stackblitz and uncomment line 8.

So, I implemented the ControlValueAccessor interface and put the formControlName directive on my component tag. This allows the value stuff to work correctly, but the validation state is not being propagated up to the mat-form-field. This is evident if you compare the UX of the username field to the password one. I’m unsure whether this is a bug in Material, or a bug in my component, or what? Again, I’m just wrapping a material input, so I don’t feel like I should have to write a ton of code to have the validation just work.

What are the steps to reproduce?

StackBlitz: https://stackblitz.com/edit/angular-material-d6myox

What is the use-case or motivation for changing an existing behavior?

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

Material beta 12

Is there anything else we should know?

This feels like a common use case for sharing common types of inputs. If this is not the correct way to accomplish this, what is a better strategy?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
josephperrottcommented, Feb 22, 2018

I am going to close this issue as this is actually something that is defined at the Angular platform level rather than Material specific. Unfortunately due to the multi ways that forms can be done, along with form elements not having to appear in forms themselves its not something that really can be set up to be convenienced at a library level.

What you will need to do is pass in the form that your FormControl will register into. I forked your stackblitz to a working version here

The highlights are:

  • In app usage:
<form [formGroup]="form">
  ...
  <my-password-2 [form]="form"></my-password-2>
</form>
  • Reusable component template:
<mat-form-field [formGroup]="form">
  <input formControlName="password" required type="text">
</mat-form-field>
  • Reusable component class:
export class ClassName {
  @Input() form: FormGroup;
  ...
}

0reactions
angular-automatic-lock-bot[bot]commented, Sep 10, 2019

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get a custom MatFormFieldControl to reflect an invalid ...
Your implementation is looking good and you are always getting the invalid as false since you did not add any validation.
Read more >
Angular Custom Form Controls - Complete Guide
In this guide, we are going to learn exactly how to take an existing custom form control component and make it fully compatible...
Read more >
Creating a custom form field control compatible with Reactive ...
We will start by creating the component, as any other component in angular. No connection with reactive forms and angular material yet.
Read more >
ControlValueAccessor - Angular
This method is called by the forms API on initialization to update the form model when values propagate from the view to the...
Read more >
Adding Integrated Validation to Custom Form Controls in ...
We want our captcha component to integrate with Angular forms, so first, ... This will make sure it will not be present in...
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