Angular 2 nested forms with child components and validation

后端 未结 6 1255
醉话见心
醉话见心 2021-02-05 13:16

I\'m trying achieve a nested form with validation in Angular 2, I\'ve seen posts and followed the documentation but I\'m really struggling, hope you can point me in the right di

相关标签:
6条回答
  • 2021-02-05 13:30

    To get a reference to the parent form simply use this (maybe not available in Angular 2. I've tested it with Angular 6):

    TS

    import {
       FormGroup,
       ControlContainer,
       FormGroupDirective,
    } from "@angular/forms";
    
    @Component({
      selector: "app-leveltwo",
      templateUrl: "./leveltwo.component.html",
      styleUrls: ["./leveltwo.component.sass"],
      viewProviders: [
        {
          provide: ControlContainer,
          useExisting: FormGroupDirective
        }
      ]
    })
    export class NestedLevelComponent implements OnInit {
      //form: FormGroup;
    
      constructor(private parent: FormGroupDirective) {
         //this.form = form;
      }
    }
    

    HTML

    <input type="text" formControlName="test" />
    
    0 讨论(0)
  • 2021-02-05 13:33

    An alternative to the FormGroupDirective (as described in @blacksheep's answer) is the use of ControlContainer like so:

    import { FormGroup, ControlContainer } from "@angular/forms";
    
    export class ChildComponent implements OnInit {
    
      formGroup: FormGroup;
    
      constructor(private controlContainer: ControlContainer) {}
    
      ngOnInit() {
        this.formGroup = <FormGroup>this.controlContainer.control;
      }
    

    The formGroup can be set in the direct parent or further up (parent's parent, for example). This makes it possible to pass a from group over various nested components, without the need for a chain of @Input()s to pass the formGroup along. In any parent set the formGroup to make it available via ControlContainer in the child:

    <... [formGroup]="myFormGroup">
    
    0 讨论(0)
  • 2021-02-05 13:37

    Not going to lie, don't know how I didn't find this post earlier.

    Angular 2: Form containing child component

    The solution is to bind the children component to the same formGroup, by passing the formGroup from the parent to the children as an Input.

    If anyone shares a piece of code to solve the problem in other way, I'll gladly accept it.

    0 讨论(0)
  • 2021-02-05 13:53
    import { Directive } from '@angular/core';
    import { ControlContainer, NgForm } from '../../../node_modules/@angular/forms';
    
    @Directive({
      selector: '[ParentProvider]',
      providers: [
        {
        provide: ControlContainer,
        useFactory: function (form: NgForm) {
          return form;
        },
        deps: [NgForm]
        }`enter code here`
      ]
    })
    export class ParentProviderDirective {
    
      constructor() { }
    
    }
    <div ParentProvider >
      for child
    </div>
    
    0 讨论(0)
  • 2021-02-05 13:54

    The main idea is that you have to treat the formGroup and formControls as variables, mainly javascript objects and arrays.

    So I'll put some code in to make my point. The code below is somewhat like what you have. The form is constructed dynamically, just that it is split into sections, each section containing its share of fields and labels.

    The HTML is backed up by typescript classes. Those are not here as they do not have much special. Just the FormSchemaUI, FormSectionUI and FormFieldUI are important.

    Treat each piece of code as its own file.

    Also please take note that formSchema: FormSchema is a JSON object that I receive from a service. Any properties of the UI classes that you do not see defined are inherited from their base Data clases. Those are not presented here. The hierarchy is: FormSchema contains multiple sections. A section contains multiple fields.

    <form (ngSubmit)="onSubmit()" #ciRegisterForm="ngForm" [formGroup]="formSchemaUI.MainFormGroup">
        <button kendoButton (click)="onSubmit(ciRegisterForm)" [disabled]="!canSubmit()"> Register {{registerPageName}} </button>
        <br /><br />
        <app-ci-register-section *ngFor="let sectionUI of formSchemaUI.SectionsUI" [sectionUI]="sectionUI">
        </app-ci-register-section>
        <button kendoButton (click)="onSubmit(ciRegisterForm)" [disabled]="!canSubmit()"> Register {{registerPageName}} </button>
    </form>
    

    =============================================

    <div class="row" [formGroup]="sectionUI.MainFormGroup">
        <div class="col-md-12  col-lg-12" [formGroupName]="sectionUI.SectionDisplayId">
            <fieldset class="section-border">
                <legend class="section-border">{{sectionUI.Title}}</legend>
                <ng-container *ngFor='let fieldUI of sectionUI.FieldsUI; let i=index; let even = even;'>
                    <div class="row" *ngIf="even">
                        <ng-container>
                            <div class="col-md-6  col-lg-6" app-ci-field-label-tuple [fieldUI]="fieldUI">
    
                            </div>
                        </ng-container>
                        <ng-container *ngIf="sectionUI.Fields[i+1]">
                            <div class="col-md-6  col-lg-6" app-ci-field-label-tuple [fieldUI]="sectionUI.FieldsUI[i+1]">
    
                            </div>
                        </ng-container>
                    </div>
                </ng-container>           
            </fieldset>
        </div>
    </div>
    

    =============================================

    {{fieldUI.Label}}

    =============================================

    <ng-container>
        <div class="row">
            <div class="col-md-4 col-lg-4 text-right">
                <label for="{{fieldUI.FieldDisplayId}}"> {{fieldUI.Label}} </label>
            </div>
            <div class="col-md-8 col-lg-8">
                <div app-ci-field-edit [fieldUI]="fieldUI" ></div>
            </div>
        </div>       
    </ng-container>
    

    =============================================

    <ng-container [formGroup]="fieldUI.ParentSectionFormGroup">    
        <ng-container *ngIf="fieldUI.isEnabled">         
            <ng-container [ngSwitch]="fieldUI.ColumnType">            
                <input *ngSwitchCase="'HIDDEN'" type="hidden" id="{{fieldUI.FieldDisplayId}}" [value]="fieldUI.Value" />
                <ci-field-textbox *ngSwitchDefault
                                  [fieldUI]="fieldUI"
                                  (valueChange)="onValueChange($event)"
                                  class="fullWidth" style="width:100%">
                </ci-field-textbox>
            </ng-container>       
        </ng-container>
    </ng-container>
    

    =============================================

    export class FormSchemaUI extends FormSchema { 
    
        SectionsUI: Array<FormSectionUI>;
        MainFormGroup: FormGroup;
    
        static fromFormSchemaData(formSchema: FormSchema): FormSchemaUI {
            let formSchemaUI = new FormSchemaUI(formSchema);
            formSchemaUI.SectionsUI = new Array<FormSectionUI>();
            formSchemaUI.Sections.forEach(section => {
                let formSectionUI = FormSectionUI.fromFormSectionData(section);
    
                formSchemaUI.SectionsUI.push(formSectionUI);
            });
            formSchemaUI.MainFormGroup = FormSchemaUI.buildMainFormGroup(formSchemaUI);        
            return formSchemaUI;
        }
    
        static buildMainFormGroup(formSchemaUI: FormSchemaUI): FormGroup {
            let obj = {};
            formSchemaUI.SectionsUI.forEach(sectionUI => {
                obj[sectionUI.SectionDisplayId] = sectionUI.SectionFormGroup;
            });
            let sectionFormGroup = new FormGroup(obj);
            return sectionFormGroup;
        }
    }
    

    =============================================

    export class FormSectionUI extends FormSection {
    
        constructor(formSection: FormSection) {        
            this.SectionDisplayId = 'section' + this.SectionId.toString();
        }
    
        SectionDisplayId: string;
        FieldsUI: Array<FormFieldUI>;
        HiddenFieldsUI: Array<FormFieldUI>;
        SectionFormGroup: FormGroup;
        MainFormGroup: FormGroup;
        ParentFormSchemaUI: FormSchemaUI;
    
        static fromFormSectionData(formSection: FormSection): FormSectionUI {
            let formSectionUI = new FormSectionUI(formSection);
            formSectionUI.FieldsUI = new Array<FormFieldUI>();
            formSectionUI.HiddenFieldsUI = new Array<FormFieldUI>();
            formSectionUI.Fields.forEach(field => {
                let fieldUI = FormFieldUI.fromFormFieldData(field);
                if (fieldUI.ColumnType != 'HIDDEN')
                    formSectionUI.FieldsUI.push(fieldUI);
                else formSectionUI.HiddenFieldsUI.push(fieldUI);
            });
            formSectionUI.SectionFormGroup = FormSectionUI.buildFormSectionFormGroup(formSectionUI);
            return formSectionUI;
        }
    
        static buildFormSectionFormGroup(formSectionUI: FormSectionUI): FormGroup {
            let obj = {};
            formSectionUI.FieldsUI.forEach(fieldUI => {
                obj[fieldUI.FieldDisplayId] = fieldUI.FieldFormControl;
            });
            let sectionFormGroup = new FormGroup(obj);
            return sectionFormGroup;
        }
    }
    

    =============================================

    export class FormFieldUI extends FormField {    
    
        constructor(formField: FormField) {
        super();
        this.FieldDisplayId = 'field' + this.FieldId.toString();       
    
        this.ListItems = new Array<SelectListItem>();        
       }
    
        public FieldDisplayId: string;
    
        public FieldFormControl: FormControl;
        public ParentSectionFormGroup: FormGroup;
        public MainFormGroup: FormGroup;
        public ParentFormSectionUI: FormSectionUI;  
    
        public ValueChange: EventEmitter<any> = new EventEmitter<any>();    
    
        static buildFormControl(formFieldUI:FormFieldUI): FormControl {
            let nullValidator = Validators.nullValidator;
    
            let fieldKey: string = formFieldUI.FieldDisplayId; 
            let fieldValue: any;
            switch (formFieldUI.ColumnType) {            
                default:
                    fieldValue = formFieldUI.Value;
                    break;
            }
            let isDisabled = !formFieldUI.IsEnabled;
            let validatorsArray: ValidatorFn[] = new Array<ValidatorFn>();
            let asyncValidatorsArray: AsyncValidatorFn[] = new Array<AsyncValidatorFn>();
    
            let formControl = new FormControl({ value: fieldValue, disabled: isDisabled }, validatorsArray, asyncValidatorsArray);
            return formControl;
        }
    }
    
    0 讨论(0)
  • 2021-02-05 13:55

    you can add an Input in your children component to pass the FormGroup to it.and use FormGroupName to pass the name of your FormGroup :)

    children.component.ts

    @Input('group');
    private dummyGroup: FormGroup;
    

    parent.component.html

    <div [formGroup]="userForm" novalidate>
        <div>
            <label>User Id</label>
            <input formControlName="userId">
        </div>
        <div formGroupName="dummy">
            <dummy [group]="userForm.controls['dummy']"></dummy>
        </div>
    </div>
    
    0 讨论(0)
提交回复
热议问题