primeng icon indicating copy to clipboard operation
primeng copied to clipboard

update angular11 reactive from error Type 'AbstractControl' is not assignable to type 'FormControl'

Open xylplm opened this issue 5 years ago • 16 comments

After angular11, stricttemplates will be added to tsconfig by default "angularCompilerOptions": { "strictInjectionParameters": true, "strictInputAccessModifiers": true, "strictTemplates": true } The checkbox component of primeng uses reactive forms <p-checkbox name="group1" value="San Francisco" [formControl]="myFormGroup.controls['cities']" inputId="sf" ></p-checkbox> An error will be reported when starting: Type 'AbstractControl' is not assignable to type 'FormControl'. But primeng recommends it. https://primefaces.org/primeng/showcase/#/checkbox

I made a demo project: https://github.com/luoxiao-xy/primeng-checkbox-demo-error Welcome clone to come down and have a look. What's the problem!

xylplm avatar Dec 10 '20 03:12 xylplm

bad bug

wheatFu avatar Dec 10 '20 03:12 wheatFu

same for Angular v10 version and PrimeNg v10 with

"angularCompilerOptions": { "fullTemplateTypeCheck": true, "strictTemplates": true }

it throws compile error

kudarias avatar Dec 16 '20 09:12 kudarias

This also impacts all the input properties of (almost) all the components. If an input property is bound to a an object property that can be null or undefined, the compiler complains about the type not being assignable.

error TS2322: Type 'string | undefined' is not assignable to type 'string'.
  Type 'undefined' is not assignable to type 'string'.

24         [styleClass]="styleClass"
           ~~~~~~~~~~~~~~~~~~~~~~~~~

FabienDehopre avatar Jan 05 '21 15:01 FabienDehopre

I managed to have a working checkbox when I replace [formControl] with formControlName + value. For example:

<p-checkbox label="Remember me" name="rememberMe" id="rememberMe" class="form-control" formControlName="rememberMe" value="rememberMe" [binary]="true" ></p-checkbox>

It is not according to primeng recommendations, but it works for me (in Angular 11).

SpiralUp avatar Jan 18 '21 06:01 SpiralUp

Same here.

rsjunior avatar Jan 18 '21 18:01 rsjunior

I managed to fix the error by adding:

"compilerOptions": { "strictPropertyInitialization": false, }

"angularCompilerOptions": { "enableIvy": false }

lauratondi avatar Feb 12 '21 15:02 lauratondi

can you guys try this ? termsAgreed: [false, Validators.requiredTrue] <---- requiredTrue / False <p-checkbox formControlName="termsAgreed" [binary]="true"> also my strictPropertyInitialization is set to false.

mafina avatar Feb 12 '21 16:02 mafina

just disable strictTemplates in your tsconfig

"strictTemplates": false

khalid7487 avatar Feb 22 '21 05:02 khalid7487

@khalid7487 @houcemlaw I know how to configure it!It is proposed to solve this problem, not to avoid it. After all, the default configuration of angular is now. It has to be solved in the end

xylplm avatar Feb 22 '21 05:02 xylplm

You could use the pseudo cast function https://angular.io/guide/template-typecheck#disabling-type-checking-using-any

<p-checkbox name="group1" value="San Francisco" [formControl]="$any(myFormGroup).controls['cities']" inputId="sf" ></p-checkbox>```

sengmann avatar Feb 26 '21 12:02 sengmann

You could use the pseudo cast function https://angular.io/guide/template-typecheck#disabling-type-checking-using-any

<p-checkbox name="group1" value="San Francisco" [formControl]="$any(myFormGroup).controls['cities']" inputId="sf" ></p-checkbox>```

Looks like this fixes the compiler error. However ".setValue(true)" still does not work

captain01010101 avatar May 06 '21 13:05 captain01010101

I use vs code, I create this FormGroup

myForm = new FormGroup({ name: new FormControl(''), age: new FormControl(''), address: new FormControl(''), Job: new FormControl('') });

shows an error in age, address, job ERROR in src/app/client/client.component.ts:14:24 - error TS2345: Argument of type 'string' is not assignable to parameter of type '{ [key: string]: AbstractControl; }'. 14 age: new FormGroup(''), ~~ src/app/client/client.component.ts:15:28 - error TS2345: Argument of type 'string' is not assignable to parameter of type '{ [key: string]: AbstractControl; }'. 15 address: new FormGroup(''), ~~ src/app/client/client.component.ts:16:24 - error TS2345: Argument of type 'string' is not assignable to parameter of type '{ [key: string]: AbstractControl; }'.
16 job: new FormGroup(''),

I just remove these three controls and write it again working well.

developernader avatar May 10 '21 00:05 developernader

I was able to get around this issue by giving the template direct access to the FormControl as well as adding it to the form in initForm().

.ts

form: FormGroup = new FormGroup({});
quantity: FormControl = new FormControl(Quantity.MIN, [
  Validators.min(Quantity.MIN),
  Validators.max(Quantity.MAX),
]);

ngOnInit(): void {
  this.initForm();
}

initForm(): void {
  this.form.addControl('quantity', this.quantity);
}

.html (In my case I am passing the FormControl quantity to a custom component that takes a FormControl Input() named "control".)

<form [formGroup]="form" (ngSubmit)="submit()">
  <app-select-quantity [control]="quantity"></app-select-quantity>
  <div>
    <button
      mat-flat-button
      type="submit"
      color="primary"
      [disabled]="!form.valid"
    >
      Submit
    </button>
  </div>
</form>

steven-rothwell avatar Jul 26 '21 01:07 steven-rothwell

I use this pattern for reactive forms. With the formModel property you can access all of your form controls in your template.

formModel: {[key in keyof Todo]?: FormControl} = {
    name: new FormControl(null, Validators.required),
    assignedTo: new FormControl(null),
    description: new FormControl(null, Validators.minLength(10)),
    due: new FormControl(null),
  };
  form: FormGroup = new FormGroup(this.formModel);

  constructor() { }

  ngOnInit() {
    this.form.patchValue(this.todoItem);
  }
<input type="text" placeholder="Name" [formControl]="formModel.name">
<textarea placeholder="description" [formControl]="formModel.description"></textarea>
<input type="date" placeholder="due date" [formControl]="formModel.due">
<input type="email" placeholder="Email" [formControl]="formModel.assignedTo">

sengmann avatar Jul 26 '21 06:07 sengmann

also you can define ypur form like this :-) form: any;

amirhosein5858 avatar Aug 30 '21 01:08 amirhosein5858

Hi,

So sorry for the delayed response! Improvements have been made to many components recently, both in terms of performance and enhancement. Therefore, this improvement may have been developed in another issue ticket without realizing it. You can check this in the documentation. If there is no improvement on this, can you reopen the issue so we can include it in our roadmap? Please don't forget to add your feedback as a comment after reopening the issue. These will be taken into account by us and will contribute to the development of this feature. Thanks a lot for your understanding!

Best Regards,

mertsincan avatar Nov 09 '22 20:11 mertsincan