update angular11 reactive from error Type 'AbstractControl' is not assignable to type 'FormControl'
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!
bad bug
same for Angular v10 version and PrimeNg v10 with
"angularCompilerOptions": {
"fullTemplateTypeCheck": true,
"strictTemplates": true
}
it throws compile error
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"
~~~~~~~~~~~~~~~~~~~~~~~~~
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).
Same here.
I managed to fix the error by adding:
"compilerOptions": {
"strictPropertyInitialization": false,
}
"angularCompilerOptions": { "enableIvy": false }
can you guys try this ? termsAgreed: [false, Validators.requiredTrue] <---- requiredTrue / False <p-checkbox formControlName="termsAgreed" [binary]="true"> also my strictPropertyInitialization is set to false.
just disable strictTemplates in your tsconfig
"strictTemplates": false
@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
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>```
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
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.
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>
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">
also you can define ypur form like this :-)
form: any;
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,