Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Angular generic forms

Angular generic forms

How many times have you found yourself implementing the same forms functionality over and over again? Same validations, same error messages and even the same forms designs.
How many times have you copied the whole form because you were using the same fields but in a different context?
Let's build our custom re-usable form inputs and create truly generic forms using Angular.

Eliran Eliassy

July 13, 2019
Tweet

More Decks by Eliran Eliassy

Other Decks in Technology

Transcript

  1. About mySelf • Experienced FE developer, specialised in B2C applications

    • Founder & developer @ e-square.io • Angular-IL & AngularUP conf CO-organiser • Writer for AngularInDepth
  2. Objective 1 reusable form control Requirements: • Should support type

    text and password • Should support validations for required and pattern • Should show UI indications • Show show error messages
  3. export class FirstCustomInputComponent { @Input() type = 'text'; @Input() isRequired:

    boolean = false; @Input() pattern: string = null; @Input() label: string = null; @Input() placeholder: string; @Input() errorMsg: string; } Reusable Custom Input
  4. <div class="form-label-group"> <input class="form-control" [type]="type" #input [placeholder]="placeholder" ngModel> <div class="d-flex">

    <span class="v" *ngIf="input.valid"> <img src="./assets/images/v.svg"> </span> <label class="mr-auto">{{label}} <span class="required" *ngIf="isRequired">*</span> </label> <span class="error" *ngIf="!input.valid">{{errorMsg}}</span> </div> </div> Reusable Custom Input
  5. <div class="form-label-group"> <input class="form-control" [type]="type" #input [placeholder]="placeholder" ngModel> <div class="d-flex">

    <span class="v" *ngIf="input.valid"> <img src="./assets/images/v.svg"> </span> <label class="mr-auto">{{label}} <span class="required" *ngIf="isRequired">*</span> </label> <span class="error" *ngIf="!input.valid">{{errorMsg}}</span> </div> </div> Reusable Custom Input
  6. <div class="form-label-group"> <input class="form-control" [type]="type" #input [placeholder]="placeholder" ngModel> <div class="d-flex">

    <span class="v" *ngIf="input.valid"> <img src="./assets/images/v.svg"> </span> <label class="mr-auto">{{label}} <span class="required" *ngIf="isRequired">*</span> </label> <span class="error" *ngIf="!input.valid">{{errorMsg}}</span> </div> </div> Reusable Custom Input
  7. <div class="form-label-group"> <input class="form-control" [type]="type" #input [placeholder]="placeholder" ngModel> <div class="d-flex">

    <span class="v" *ngIf="input.valid"> <img src="./assets/images/v.svg"> </span> <label class="mr-auto">{{label}} <span class="required" *ngIf="isRequired">*</span> </label> <span class="error" *ngIf="!input.valid">{{errorMsg}}</span> </div> </div> Reusable Custom Input
  8. Using the custom form control <form class="form-signin" (ngSubmit)="onSubmit(f.value)" #f="ngForm"> <app-first-custom-input

    [placeholder]="'Email'" [isRequired]="true" [errorMsg]="'Please enter your name'" [label]="'User Email'" [pattern]="'[A-Za-z0-9._%-]+@[A-Za-z0-9._%-]+\\.[a-z]{2,3}'" ngModel name="email"> </app-first-custom-input> <button class="btn btn-lg btn-primary btn-block" [disabled]="!f.valid" type="submit"> Sign in </button> </form>
  9. Using the custom form control <form class="form-signin" (ngSubmit)="onSubmit(f.value)" #f="ngForm"> <app-first-custom-input

    [placeholder]="'Email'" [isRequired]="true" [errorMsg]="'Please enter your name'" [label]="'User Email'" [pattern]="'[A-Za-z0-9._%-]+@[A-Za-z0-9._%-]+\\.[a-z]{2,3}'" ngModel name="email"> </app-first-custom-input> <button class="btn btn-lg btn-primary btn-block" [disabled]="!f.valid" type="submit"> Sign in </button> </form>
  10. Using the custom form control export interface ControlValueAccessor { writeValue(obj:

    any): void; registerOnChange(fn: any): void; registerOnTouched(fn: any): void; setDisabledState?(isDisabled: boolean): void; }
  11. Objective 2- reusable form Requirements: • Create an Address form

    which can be reused. • Implement the form - UI, controls, Validations - just once.
  12. • The ControlValueAccessor as a bridge between Angular forms API

    to our custom controls and reusable forms. • How the DI can help us to inject the NgControl in order to get the control reference. • How we can leverage the DI to create sub forms. Summary