[Angular2 Form] Nested formGroup, and usage of formGroupName

We can nest formGorup:

    this.reactiveForm = fb.group({
      username: [
        ‘‘,
        [
          Validators.required,
          Validators.minLength(3)
        ]
      ],
      pwds: fb.group({
        pwd: ‘‘,
        rpwd: ‘‘
      }, {validator: passwordValidator})
    });

We make password as an own group. So in html, we need to use formGroupName istead of formControlName.

<form [formGroup]="reactiveForm" novalidate autocomplete="off">
  <div class="form-field">
    <label>Username:</label>
    <input formControlName="username">
    <div class="field-error-message" *ngIf="reactiveForm.controls.title.errors?.required">
      Username is required
    </div>
  </div>

  <div formGroupName="pwds">
    <div class="form-field">
      <label>pwd</label>
      <input formControlName="pwd">
    </div>
    <div class="form-field">
      <label>rpwd</label>
      <input formControlName="rpwd">
    </div>
  </div>
</form>

And how we check the value or errors?:

<pre>
  {{reactiveForm.get(‘pwds‘)?.value | json}}
  {{reactiveForm.get(‘pwds‘)?.errors | json}}
</pre>

And we also passwordValidator haven‘t cover yet, it is just a fucntion:

function passwordValidator(c: AbstractControl){
  return c.get(‘pwd‘).value === c.get(‘rpwd‘).value ?
    null : // valid
    { //invalid
      nomatch: true
    }
}

And notice that we put this validator inside the nested group, so we can get nice error effect:

时间: 2024-07-31 14:25:54

[Angular2 Form] Nested formGroup, and usage of formGroupName的相关文章

[Angular2 Form] Create custom form component using Control Value Accessor

//switch-control component import { Component } from '@angular/core'; import { ControlValueAccessor, NG_VALUE_ACCESSOR, NG_VALIDATORS, Validators} from '@angular/forms'; @Component({ selector: 'switch-control', templateUrl: './switch-control.componen

[Angular2 Form] Use RxJS Streams with Angular 2 Forms

Angular 2 forms provide RxJS streams for you to work with the data and validity as it flows out of the forms. These streams allow you handle complex scenarios and asynchronous scenarios with relative ease. This example shows you how to log out the va

[Angular2 Form] Check password match

Learn how to create a custom validator to check whether passwords match. <h1>password match</h1> <form novalidate autocomplete="off" [formGroup]="signupForm"> <div class="form-field"> <label>Pass

[Angular2 Form] patchValue, setValue and reset() for Form

Learn how to update part of form model, full form model and reset whole form. We have form definetion like this: reactiveForm: FormGroup; constructor(fb: FormBuilder) { this.extra = new FormControl('...', [ Validators.maxLength(100) ]); this.reactive

[Angular2 Form] Reactive Form, FormBuilder

Import module: import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { MessageComponent } from './message.component'; import messageRoutes from './message.routes'; import {FormsModule, ReactiveFormsModule} f

[Angular2 Form] Reactive Form, show error message for one field

<form [formGroup]="reactiveForm" novalidate autocomplete="off"> <div class="form-field"> <label>Title:</label> <input formControlName="title"> <div class="field-error-message&qu

[Angular2 Form] Build Select Dropdowns for Angular 2 Forms

Select Dropdowns in Angular 2 a built with select and option elements. You use *ngFor to loop through your values and create options and use ngModel to keep track of the value as it changes. <form #formRef="ngForm"> <select name="l

[Angular2 Form] Model Driven Form Custom Validator

In this tutorial we are going to learn how simple it is to create custom form field driven validators while using Angular 2 model driven forms. These type of validators are really just plain functions that follow a set of conventions. We are going to

[Angular2 Form] Angular 2 Template Driven Form Custom Validator

In this tutorial we are going to learn how we can also implement custom form field validation in Angular 2 template driven forms, by creating our own custom validation directive. We are going to see how to write such directive and how its a best prac