formgroups

FormControl validator always invalid

≡放荡痞女 提交于 2021-02-10 18:43:53
问题 Following my previous question, I'm trying to create a custom validator that allow the users to type only specific values in an input of text. app.component.ts: export class AppComponent implements OnInit { myForm: FormGroup; allowedValuesArray = ['Foo', 'Boo']; ngOnInit() { this.myForm = new FormGroup({ 'foo': new FormControl(null, [this.allowedValues.bind(this)]) }); } allowedValues(control: FormControl): {[s: string]: boolean} { if (this.allowedValuesArray.indexOf(control.value)) { return

Angular Reactive Forms in Reusable Child Components: Problem with FormArrays: `formGroupName` must be used with a parent formGroup directive

馋奶兔 提交于 2021-02-06 14:01:02
问题 I'am trying to describe the problem on an example (full example on stackblitz) If I try to place some parts of reactive-form in the form of simple "formControls" or "formGroups" withing child-components, there are no problems. (See the example on stackblitz above). FormGroupDirective works as expected. But If I try to place a FormArray within a child-component, I get troubles because of: <div [formGroupName]="i"> <!-- Error: formGroupName must be used with a parent formGroup directive. -->

Angular Reactive Forms in Reusable Child Components: Problem with FormArrays: `formGroupName` must be used with a parent formGroup directive

核能气质少年 提交于 2021-02-06 13:50:52
问题 I'am trying to describe the problem on an example (full example on stackblitz) If I try to place some parts of reactive-form in the form of simple "formControls" or "formGroups" withing child-components, there are no problems. (See the example on stackblitz above). FormGroupDirective works as expected. But If I try to place a FormArray within a child-component, I get troubles because of: <div [formGroupName]="i"> <!-- Error: formGroupName must be used with a parent formGroup directive. -->

Angular 7 and form arrays error of cannot find a control with path

时间秒杀一切 提交于 2021-01-28 11:25:32
问题 I am building a form group with an array inside of it, using mat-table data source. I started by creating the table: <form *ngIf="formGroup" [formGroup]="formGroup"> <table mat-table [dataSource]="dataSource" *ngIf="total>0" formArrayName="distribution"> <ng-container matColumnDef="ind_id"> <th mat-header-cell *matHeaderCellDef> ID </th> <td mat-cell *matCellDef="let element">{{element.individual_id}}</td> </ng-container> <ng-container matColumnDef="ind_name"> <th mat-header-cell

Angular Reactive Form with dynamic fields

我怕爱的太早我们不能终老 提交于 2020-07-23 07:35:50
问题 I'm currently battling with Angular form array. I have a form in which I add the fields dynamically. I have created the form object: this.otherDataForm = this.fb.group({ }); I add the dynamic fields like this: addField(field: CustomFormField): void { this.otherDataForm.addControl(id_campo, new FormControl('', Validators.required)); } I loop through those fields: <form *ngIf="selectedProfile" [formGroup]="otherDataForm"> <div class="mb-3" *ngFor="let field of fields; let i = index"> <label>{