Reactive Forms
Edit Flights
In this exercise you will implement a Reactive Form to edit Flights.
-
Create a
FlightEditComponentwhich belongs to theFlightBookingModule:ng generate component flight-booking/flight-edit -
Import the
ReactiveFormsModulein theFlightBookingModule. -
If no router is used in your Angular App yet, you need to export your Component and use the Component's tag in your
AppComponent's Template. -
Your
FlightEditComponentshall defineFormGroupnamededitForm.Show code
[…] import {FormGroup} from '@angular/forms'; @Component({[…]}) export class FlightEditComponent implements OnInit { editForm: FormGroup = this.getInitialEditForm(); […] } -
Request the
FormBuildervia Dependency Injection in theFlightEditComponent.Show code
import {[…], FormBuilder} from '@angular/forms'; @Component({ […] }) export class FlightEditComponent implements OnInit { […] constructor(private fb: FormBuilder) { } […] } -
Use the
FormsBuilderin the new methodgetInitialEditForm()method to create aFormGroupwhich represents a Flight and assign it to theeditFormproperty.Show code
export class FlightEditComponent implements OnInit { editForm: FormGroup = this.getInitialEditForm(); […] getInitialEditForm(): FormGroup { return this.fb.group({ id: [1], from: [], to: [], date: [] }); } […] } -
Use the code completion feature of your IDE to see which properties and methods are available for the
editFormproperty. Useconsole.log()to output information likevalue,valid,touchedordirty.Show code
export class FlightEditComponent implements OnInit { […] save(): void { console.log('value', this.editForm.value); console.log('valid', this.editForm.valid); console.log('touched', this.editForm.touched); console.log('dirty', this.editForm.dirty); } […] } -
subscribe()toeditForm.valueChangeswhich is of typeObservableand log this value to the console as well. You will get notified each time one of the form inputs change.Show code
export class FlightEditComponent implements OnInit { […] ngOnInit() { […] this.editForm.valueChanges.subscribe(v => { console.log('changes', v); }); } […] } -
Open the file
flight-edit.component.htmland create the necessary HTML markup for your form. The form needs to be connected to yourFormGroupsby using the propertyeditForm.Show code
<form [formGroup]="editForm"> <div class="form-group"> <label>Id:</label> <input formControlName="id" class="form-control"> </div> <div class="form-group"> <label>Date:</label> <input formControlName="date" class="form-control"> </div> <div class="form-group"> <label>From:</label> <input formControlName="from" class="form-control"> </div> <div class="form-group"> <label>To:</label> <input formControlName="to" class="form-control"> </div> <div class="form-group"> <button (click)="save()" class="btn btn-default">Save</button> </div> </form> -
Test your application.
Build-in Validators
Now you will add the Build-in Validators required and minlength to your Reactive Forms implementation.
-
Open the file
flight-edit.component.tsand add those Validators to theFormGroup.minlengthshall be set to3.Show code
[…] getInitialEditForm(): FormGroup { return this.fb.group({ id: [1], from: [null, [Validators.required, Validators.minLength(3)]], to: [null], date: [null] }); } […] -
Open the file
flight-edit.component.htmland add the HTML markup to show errors for theFormControlfrom.Show code
<input formControlName="from"> […] errors: {{ editForm.controls['from'].errors | json }} -
Use the
hasError()method of theFormControlto find out whether theminlengtherror occurred.Show code
<input formControlName="from" […] > […] <div class="alert alert-danger" *ngIf="editForm.controls['from'].hasError('minlength')"> ...minlength... </div>