Chapter 1 - Model Driven Forms

Similar documents
Advantages: simple, quick to get started, perfect for simple forms, don t need to know how form model objects work

ANGULAR2 OVERVIEW. The Big Picture. Getting Started. Modules and Components. Declarative Template Syntax. Forms

Learn to Build Awesome Apps with Angular 2

Integrating Angular with ASP.NET Core RESTful Services. Dan Wahlin

One Framework. Angular

Course Outline. ProTech Professional Technical Services, Inc. Comprehensive Angular 7 Course Summary. Description

"Charting the Course... Comprehensive Angular 5. Course Summary

"Charting the Course... Comprehensive Angular 6 Course Summary

By the end of this Angular 6 tutorial, you'll learn by building a real world example application:

Extending VMware vcloud Director User Interface Using Portal Extensibility Ticketing Example

Upgrading to Ionic 3 APPENDIX D. Angular 4

Before proceeding with this tutorial, you should have a basic understanding of HTML, CSS, JavaScript, TypeScript, and Document Object Model (DOM).

Comprehensive Angular 2 Review of Day 3

IN4MATX 133: User Interface Software

Learn to Build Awesome Apps with Angular 2

The core of Tapestry's form support is the Form component. The Form component encloses (wraps around) all the other field components such

Source. Developer Guide / forms

Mobile App Development

Introduction to. Angular. Prof. Dr.-Ing. Thomas Wiedemann.

Angular 4 Training Course Content

Frontend UI Training. Whats App :

3 Days Training Program

Setting up an Angular 4 MEAN Stack (Tutorial)

ANGULARJS - MOCK TEST ANGULARJS MOCK TEST II

ANGULAR 2.X,4.X + TYPESRCIPT by Sindhu

Angular 2 and TypeScript Web Application Development

Static Webpage Development

ANGULARJS INTERVIEW QUESTIONS

Lab 1 - Introduction to Angular

Angular 4 Syllabus. Module 1: Introduction. Module 2: AngularJS to Angular 4. Module 3: Introduction to Typescript

Sample Copy. Not For Distribution.

DNCMagazine. ANGULAR. Cheat Sheet

PHP WITH ANGULAR CURRICULUM. What you will Be Able to Achieve During This Course

Single Page Applications using AngularJS

Exception Handling. Chapter 9

Frontend Web Development with Angular. CC BY-NC-ND Carrot & Company GmbH

Advanced React JS + Redux Development

PHP + ANGULAR4 CURRICULUM 6 WEEKS

Angular 2: What s new? Jonas Bandi, IvoryCode GmbH

Angular 2 Programming

VALIDATING USING REGULAR EXPRESSIONS Understanding Regular Expression Characters:

D3 + Angular JS = Visual Awesomesauce

Comprehensive AngularJS Programming (5 Days)

Creating Effective Websites using AngularJS

React + React Native. Based on material by Danilo Filgueira

JavaScript and XHTML. Prof. D. Krupesha, PESIT, Bangalore

Chapter 1 - Consuming REST Web Services in Angular

B l o c k B i n d i n g s

Input and Validation. Mendel Rosenblum. CS142 Lecture Notes - Input

About me. Routing into the Sunset with Angular. Manfred Steyer SOFTWAREarchitekt.at Trainer & Consultant Focus: Angular Google Developer Expert (GDE)

<body> <form id="myform" name="myform"> <!-- form child elements go in here --> </form> </body>

NetAdvantage for ASP.NET Release Notes

Angular from the Beginning

Angular. конфигурируем до неузнаваемости

JAVA MOCK TEST JAVA MOCK TEST II

"Charting the Course... Comprehensive Angular. Course Summary

JavaScript s role on the Web

Cross-Platform Mobile-Entwicklung mit dem Ionic Framework

MICROSOFT EXAM QUESTIONS & ANSWERS

1 CS480W Quiz 6 Solution

AngularJS. Beginner's guide - part 1

THE HITCHHIKERS GUIDE TO. Harper Maddox CTO, EdgeTheory 30 September 2014

Sample CS 142 Midterm Examination

INTRODUCTION TO.NET. Domain of.net D.N.A. Architecture One Tier Two Tier Three Tier N-Tier THE COMMON LANGUAGE RUNTIME (C.L.R.)

DAY 2. Creating Forms

AngularJS Introduction

a Very Short Introduction to AngularJS

Manual Html A Href Onclick Submit Form

Design and Implementation of Single Page Application Based on AngularJS

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Angular 2 and TypeScript Web Application Development

Understanding Angular Directives By Jeffry Houser

CSC Web Programming. Introduction to JavaScript

Financial. AngularJS. AngularJS.

What is AngularJS. à Javascript Framework à MVC à for Rich Web Application Development à by Google

Ajax- XMLHttpResponse. Returns a value such as ArrayBuffer, Blob, Document, JavaScript object, or a DOMString, based on the value of

Angular2. Bernhard Niedermayer. Software Development playing around with Angular2 since alpha.

Help Documentation. Copyright 2007 WebAssist.com Corporation All rights reserved.

OO and Ahh! An Introduction to Object Oriented Programming With PHP. Division 1 Systems. John Valance. Copyright John Valance Division 1 Systems

Financial. AngularJS. AngularJS. Download Full Version :

XML JavaScript Object Notation JSON Cookies Miscellaneous What Javascript can t do. OOP Concepts of JS

Web Application Development

ADF Code Corner How-to bind custom declarative components to ADF. Abstract: twitter.com/adfcodecorner

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Client Side JavaScript and AJAX

Helpline No WhatsApp No.:

FRONT END WEB. {< Course Details >}

Declarations and Access Control SCJP tips

About me. Google Developer Expert Telerik Developer Expert Digital

Multimedia im Netz Online Multimedia Winter semester 2015/16. Tutorial 07 Minor Subject

welcome to BOILERCAMP HOW TO WEB DEV

Manual Html A Href Onclick Submit Button

CISC 1600 Lecture 2.4 Introduction to JavaScript

PASS4TEST 専門 IT 認証試験問題集提供者

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

Part 1: jquery & History of DOM Scripting

More on JavaScript Functions

Skyway 6.3 How To: Web Services

EXAM Microsoft MTA Software Development Fundamentals. Buy Full Product.

CITS1231 Web Technologies

Transcription:

Chapter 1 - Model Driven Forms Objectives This chapter includes the following model driven forms topics: Setup FormGroup initialization FormControl object Validation SubForms 1.1 Model Driven Forms Overview Angular has two ways to define forms, "template driven" forms and "model driven" forms Model driven forms are also referred to as "Reactive forms" Model driven forms create and manipulate form control objects directly in the component class The component can observe changes in form control state and react to those changes, thus "reactive" forms There are still form elements in the template of the component but not as much code to define the form The template just binds to the form control elements defined in the component 1.2 Setup for Model Driven Forms Similar to that for template driven forms To use the full features of model driven forms, use the 'ReactiveFormsModule' instead of the regular 'FormsModule' Add the following in app.module.ts boot file: import { ReactiveFormsModule from '@angular/forms';

... @NgModule({ imports: [ BrowserModule, ReactiveFormsModule ], 1.3 Form Component Setup Typical imports required in the form component: import { FormGroup, FormControl, FormBuilder, Validators from '@angular/forms'; @Component({... ) 1.4 Setup Main FormGroup In the component class Have a property of FormGroup type to reference the form Construct the form during component construction or initialization myform: FormGroup; ngoninit(){ this.myform = new FormGroup({ first: new FormControl('Jim', []), last: new FormControl('Doe',[Validators.required]) ); In HTML Template The <form> element references the form with the [formgroup] property The individual form elements reference the form elements from the component with the 'formcontrolname' property 2

<form [formgroup]="myform"> First: <input formcontrolname="first" ><br> Last : <input formcontrolname="last" ><br> <input type=button value=submit (click)=onsubmit()> </form> 1.5 formcontrolname The formcontrolname directive is used to bind input fields in the HTML template to FormControl objects in the component class: formcontrolname directive: <input formcontrolname="first" > FormControl Object first: new FormControl('Jim', []), These two are bound by the name "first" 1.6 FormControl Object There are two ways to create the FormControl object bound to the input element in a form: Constructed during FormGroup initialization this.myform = new FormGroup({ first: new FormControl('Jim'),... Then bound to form using 'formcontrolname' <input formcontrolname="first" > Created as a separate property of the component class firstname = new FormControl('Jim'); Then bound to using '[formcontrol]' 3

<input [formcontrol]="firstname" > You can only use '[formcontrol]' in the template when the FormControl is available as a property of the component If the FormControl is only a child of the FormGroup, you must use 'formcontrolname' to reference it 1.7 Getting Form Values Defining a form is only useful if you can get the values entered in the form and use them in the rest of the Angular application In JavaScript code in the component, you can get the value of the entire form with the '.value' property of the form data = myform.value; Or of a individual component within the form using the 'get' method firstname = myform.get('firstname').value; The "value" of the entire form is an object with property names matching the form control names and values containing the form data { first: "Bob", // myform.value last: "Builder" One advantage of model driven forms is that data is available synchronously, meaning you don't have to wait for Angular template directives to update the form This also simplifies testing since you don't need to manipulate forms asynchronously in a test 4

1.8 FormBuilder Form Initialization Using the 'FormBuilder' class simplifies form initialization You don't need to manually call the FormControl constructor To use the FormBuilder to initialize a form: Import the class with the other '@angular/forms' classes Inject a FormBuilder into the component constructor constructor(private fb: FormBuilder) { this.createform(); // calling custom method Call the 'group' method of FormBuilder with an object where the properties are the names of form controls and values are the parameters you would pass if calling the FormControl constructor Multiple FormControl constructor parameters are passed as an array createform() { this.heroform = this.fb.group({ first: '', // <--- the FormControl called "first" last: ['', Validators.required ] // parameter array ); 1.9 Validation One of the biggest differences between template driven forms and model driven forms is where validation is declared Template driven forms have validation properties defined in the HTML template Model driven forms define validation by customizing the construction of 5

FormControl objects in the component When FormControl objects are created, there is an optional, second parameter to the constructor We can pass a single validator or an array of validators as the second parameter fieldname: new FormControl ( default_value, validator validator[] ) Example validators array: [ Validators.minLength(5), Validators.required ] 1.10 Built-In Validators Angular has a few built-in validators: Validators.required Validators.minLength(minLen: number) Validators.maxLength(maxLen: number) Validators.pattern(regex-pattern: string) Usage examples: name: new FormControl('', Validators.required)), street: new FormControl('', Validators.minLength(3)), city: new FormControl('', Validators.maxLength(10)), zip: new FormControl('', Validators.pattern('[A-Za-z]{5')) Notes Corresponding usage in Template Driven Forms: <form novalidate> <input type="text" name="name" ngmodel required> <input type="text" name="street" ngmodel minlength="3"> <input type="text" name="city" ngmodel maxlength="10"> <input type="text" name="zip" ngmodel pattern="[a-za-z]{5"> </form> 6

1.11 Custom Validator Custom Validators: Take a Control as input Return null when the control is valid Return an object with validation info when control is invalid Example ( email.validator.ts ): Notes import {FormControl from '@angular/forms'; export function validateemail(control: FormControl) { if( typeof(control.value) === 'string'){ if(control.value.includes("@")){ return null; else{ return {validateemail: {valid: false To create a custom validator: create a separate file for the validator import FormControl so we can reference it create and exporting the validation function have the validation function accept a FormControl parameter have the validation function return null or object The example validator above simply checks for the presence of the '@' symbol in the string. 7

1.12 Using a Custom Validator Steps: Import the validator function import { validateemail from './email.validator'; Reference the function in a FormControl this.myform = fb.group({ email: ["james@abc.com", [ validateemail ]] ); Notes: Full code of validator function ( email.validator.ts ): import {FormControl from '@angular/forms'; export function validateemail(control: FormControl) { if( typeof(control.value) === 'string'){ if(control.value.includes("@")){ return null; else{ return {validateemail: {valid: false Full code of component that uses the custom validator ( custom.validator.component.ts ): import { Component, OnInit from '@angular/core'; import { validateemail from './email.validator'; import { FormGroup, FormControl, Validators from '@angular/forms'; @Component({ selector: 'customvalform', template: ` <h3>custom Validator Form</h3> <form [formgroup]="myform"> Email: <input formcontrolname="email" ><br> <input type=button value=submit (click)=onsubmit() > 8

</form> Form Valid: {{myform.valid<br> Form Value: {{myform.value json `, ) export class CustomValidatorComponent implements OnInit { myform: FormGroup; ngoninit(){ this.myform = new FormGroup({ email: new FormControl('Jim@abc.com', [ validateemail ]), ); onsubmit(){ console.log( "Valid: " + this.myform.valid ); console.log("value: " + JSON.stringify(this.myForm.value, null, 2)); 1.13 Useful FormGroup and FormControl Properties/Functions Several properties of FormGroup and FormControl are inherited from the AbstractControl class.value.status String of the control status.disabled/.enabled.reset(...).valid/.invalid There are also '.setvalue' and '.patchvalue' methods.pristine/.dirty If the UI value has changed.touched/.untouched If the user has selected/entered These behave the same on a FormControl and change the value of the control With a FormGroup they take an object with control names as keys With 'setvalue' the object must contain exactly the form structure or 9

an error is thrown 'patchvalue' can take a super-set or sub-set of the form data and will match what it can without throwing an error 1.14 Sub FormGroups - Component Class Component Class Code: export class FormComponent implements OnInit { myform: FormGroup; ngoninit(){ this.myform = new FormGroup({ first: new FormControl('Jim', []), last: new FormControl('Doe', []), address: new FormGroup({ address: new FormControl('Main Street', []), city: new FormControl('Newark', []), state: new FormControl('NJ', []), zip: new FormControl('07102', []), ) ); onsubmit(){ console.log( JSON.stringify( this.myform.value, null, 2)); 10

1.15 Sub FormGroups - HTML Template In HTML Template <form [formgroup]="myform"> First: <input formcontrolname="first" ><br> Last : <input formcontrolname="last" ><br> <div formgroupname="address"> Address: <input formcontrolname="address" ><br> City: <input formcontrolname="city" size=12 > State: <input formcontrolname="state" size=2 > Zip: <input formcontrolname="zip" size=5 > </div> <input type=button value=submit (click)=onsubmit()> </form> In the above HTML template we have two fields (first, last) at the root level of the main form group "myform" Then we have four fields (address, city, state, zip) in the sub Form Group Address. Note the sub FormGroup is referenced by the 'formgroupname' property as 'formgroup' can only reference something declared as a component property and not the nested FormGroup 'address' 1.16 Why Use Sub FormGroups It formats the form data for us: /* this.myform.value */ { "first": "Jim", "last": "Doe", "address": { 11

"address": "Main Street", "city": "Newark", "state": "NJ", "zip": "07102" Validation can be applied separately to different sub groups. 1.17 Summary Model driven forms define more of a form's properties in the code of the related component Components with model driven forms still have form elements in the template but mainly to match up with the form elements defined in the component A FormGroup object is the root of a form with FormControl objects representing individual form elements Using the FormBuilder class can simplify the initialization of a form Validation of model driven forms is done with setting 'Validators' properties in the form controls Sub forms can model a more complex structure with FormGroup objects nested inside other FormGroups 12