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.

Use more than one CustomValueAcessor in one Input field

See original GitHub issue

I’m submitting a … (check one with β€œx”)

[x] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior I have created 2 directives with a CustomValueAcessor, and when I use them in an field I got the following error:

Error: Uncaught (in promise): Error: More than one custom value accessor matches form control with unspecified name attribute

Expected behavior The input field should accept more than 2 CustomValueAcessor

Minimal reproduction of the problem with instructions

http://plnkr.co/edit/zPz40DNYmAO7mokJ09a9?p=preview

Please tell us about your environment:

β€œdependencies”: { β€œ@angular/animations”: β€œ^4.1.0”, β€œ@angular/cli”: β€œ^1.0.0-beta.28.3”, β€œ@angular/common”: β€œ^4.1.0”, β€œ@angular/compiler”: β€œ^4.1.0”, β€œ@angular/compiler-cli”: β€œ^4.1.0”, β€œ@angular/core”: β€œ^4.1.0”, β€œ@angular/forms”: β€œ^4.1.0”, β€œ@angular/http”: β€œ^4.1.0”, β€œ@angular/platform-browser”: β€œ^4.1.0”, β€œ@angular/platform-browser-dynamic”: β€œ^4.1.0”, β€œ@angular/platform-server”: β€œ^4.1.0”, β€œ@angular/router”: β€œ^4.1.0” }

  • Angular version: 2.0.X
  • Browser: [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]
  • Language: [all | TypeScript X.X | ES6/7 | ES5]

  • Node (for AoT issues): node --version =

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:16 (3 by maintainers)

github_iconTop GitHub Comments

29reactions
remborgcommented, Dec 20, 2017

As mentioned above, text-mask won’t work with material2. A workaround is to use the vanilla version of text-mask: 1 - Import the library in your component import * as textMask from "vanilla-text-mask/dist/vanillaTextMask.js";

2 - Add your mask in the component and init your input (don’t forget to import AfterViewInit and implement it to your component)

  mask = [ /[1-9]/, /\d/, /\d/];
  maskedInputController;

  @ViewChild("input", { read: ViewContainerRef }) public input;

  ngAfterViewInit(): void {
    setTimeout(() => {
      this.maskedInputController = textMask.maskInput({
        inputElement: this.input.element.nativeElement,
        mask: this.mask
      });
    });
  }

  ngOnDestroy() {
    this.maskedInputController.destroy();
  }

3 - in your template, make sure the input element has an #input attribute

<mat-form-field class="input">
  <input matInput [matDatepicker]="picker" [(ngModel)]="selectedValue" #input>
  <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
  <mat-datepicker #picker></mat-datepicker>
</mat-form-field>
10reactions
brandon1525commented, Feb 12, 2018

i have a solution with a directive using the solution of @remborg

import { Directive, ElementRef, OnDestroy } from '@angular/core';
import * as textMask from 'vanilla-text-mask/dist/vanillaTextMask.js';

@Directive({
  selector: '[appMaskDate]'
})
export class MaskDateDirective implements OnDestroy {

  mask = [/\d/, /\d/, '/', /\d/, /\d/, '/', /\d/, /\d/, /\d/, /\d/]; // dd/mm/yyyy
  maskedInputController;

  constructor(private element: ElementRef) {
    this.maskedInputController = textMask.maskInput({
      inputElement: this.element.nativeElement,
      mask: this.mask
    });
  }

  ngOnDestroy() {
    this.maskedInputController.destroy();
  }

}

<input matInput formControlName="FechaInicio" [matDatepicker]="FechaInicioRef" placeholder="Fecha Inicio" appMaskDate>

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular 2 multiple custom value accessor - Stack Overflow
I am creating an application using angular2. I need to get a Date from user input using a calendar popover, but I need...
Read more >
2. Creating Dynamic Forms & Usage of CustomValueAccessor
It collects FormControl values and validation statuses in an array. It is used when the number of form elements is not certain. In...
Read more >
Control Value Accessor: Custom Form Components in Angular
Create custom form components in Angular and control them with a FormControl.
Read more >
Creating custom form controls using Control Value Accessor ...
We are going to take a look at the base form that we are gonna work with. We just have some basic input...
Read more >
Angular Custom Form Controls With Control Value Accessor
Notice the multi flag set to true, this means that this dependency provides a list of values, and not only one value. Now...
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