ngx-mask icon indicating copy to clipboard operation
ngx-mask copied to clipboard

Error When Used in FormArray, datepicker angular material and ngx-mask

Open silvergama opened this issue 7 years ago • 9 comments

ERROR Error: More than one custom value accessor matches form control with path: ' -> -> '

`<div [formGroup]="form"> <div formArrayName="faseConstrucao" *ngFor="let faseConstrucao of getFaseConstrucaoControl.controls; let i = index"> <div [formGroupName]="i"> <input matInput [matDatepicker]="picker" placeholder="Lançamento" [formControlName]="lancamento" mask="00/00/0000"> <mat-datepicker-toggle matSuffix [for]="picker"> <mat-datepicker #picker> <input matInput [matDatepicker]="picker2" placeholder="Encerramento" [formControlName]="encerramento" mask="00/00/0000"> <mat-datepicker-toggle matSuffix [for]="picker2"> <mat-datepicker #picker2>

`

silvergama avatar Apr 29 '19 17:04 silvergama

@silvergama could you provide a full example ?

ValeriaKochegarova avatar May 06 '19 12:05 ValeriaKochegarova

Ex: https://stackblitz.com/edit/angular-kmcusk

In app.component.html Add formControlName="date" in input to show error: More than one custom value accessor matches form control with unspecified name attribute

With FormArray: https://stackblitz.com/edit/angular-7qi6qo

silvergama avatar May 06 '19 16:05 silvergama

@silvergama Unfortunately we cannot do this , check this #50

ValeriaKochegarova avatar May 07 '19 10:05 ValeriaKochegarova

Is this bug still unfix-able ?

aemonge avatar Jun 13 '19 10:06 aemonge

@aemonge please check this #50

ValeriaKochegarova avatar Jul 04 '19 09:07 ValeriaKochegarova

Hi @ValeriaKochegarova , thanks for the link. But I already posted there another question related to this.

Basically over-there, the official solution is not to use ngMask.... so that's why I use this ticket to ask if there's a plan to fix the bug or should we follow the #50 guidelines and avoid the library at all.

aemonge avatar Jul 04 '19 09:07 aemonge

Did anything change? Any implementation? Any workaround?

LuanMaik avatar Jun 30 '20 23:06 LuanMaik

@LuanMaik it's an open source project from my reading and the maintainers have chosen to not fix it or say that it's impossible to fix. Current suggestion is to use a custom directive with a native adapter as seen in #50 .

bcordes1 avatar Sep 03 '20 17:09 bcordes1

Based on an existing solution:

https://stackoverflow.com/questions/60818529/angular-input-with-ngx-mask-matdatepicker-and-reactive-form

Hi I have a working solution:

The matDatePicker input is hidden and linked to the masked input to the hidden datepicker which binds to the formControl and bind matInput the the masked input field.

ngModelOptions is important, as there is an error thrown when using ngModel used within a form

    ngModel cannot be used to register form controls with a parent formGroup directive.

HTML:

  <input #dateInput type="hidden" id="date" formControlName="date" required [matDatepicker]="datePicker" [min]="today" /> 

  <input #maskedInput matInput (input)="dispatch(dateInput)" placeholder="Pick a date"
          [(ngModel)]="dateInput.value" [ngModelOptions]="{ standalone: true }" mask="00/00/0000"  [dropSpecialCharacters]="false" />

  <mat-datepicker-toggle  matSuffix [for]="datePicker"></mat-datepicker-toggle>

  <mat-datepicker #datePicker></mat-datepicker>

Component:

  dispatch(input: HTMLInputElement) {
    input.dispatchEvent(new Event('input'));
  }
Screenshot 2023-12-24 at 10 06 15 AM

Screenshot 2023-12-24 at 10 06 56 AM

phillip389 avatar Dec 24 '23 15:12 phillip389