Mastering Angular Reactive Forms: Build Solid Expertise in Reactive Forms using Form Control, Form Group, Form Array, Validators, Testing and more with Angular 12 Through Real-World Use Cases (English Edition)
()
About this ebook
You will learn about the basic building blocks of Angular Reactive Forms, apply a binding in all HTML form elements using Form Control, Form Group, or Form Array, and apply simple or complex validators. Furthermore, you will learn how to interact with end user's form value changes, how to Unit Test, and how to expand your form by building re-usable form parts beyond basics. The book is based on patterns that are widely used by the community and many enterprise companies.
After reading this book, you will not just upgrade your knowledge, but you will be a strong Angular developer helping enterprises with solutions in using Angular Reactive Forms.
Related to Mastering Angular Reactive Forms
Related ebooks
A Journey to Angular Development: Learn Angular Fundamentals, TypeScript, Webpack, Routing, Directives, Components, Forms, and Modules with Practical Examples Rating: 0 out of 5 stars0 ratingsStep-by-Step Angular Routing: Learn To Create client-side and Single Page Apps with Routing and Navigation Rating: 0 out of 5 stars0 ratingsAngular Services Rating: 0 out of 5 stars0 ratingsLearn AngularJS in 24 Hours Rating: 0 out of 5 stars0 ratingsParallel Programming with C# and .NET Core: Developing Multithreaded Applications Using C# and .NET Core 3.1 from Scratch Rating: 0 out of 5 stars0 ratingsNode.js High Performance Rating: 0 out of 5 stars0 ratingsMastering React Test-Driven Development: Build rock-solid, well-tested web apps with React, Redux and GraphQL Rating: 0 out of 5 stars0 ratings120 Advanced JavaScript Interview Questions Rating: 0 out of 5 stars0 ratings50 C# Concepts Every Developer Should Know Rating: 0 out of 5 stars0 ratingsTesting with JUnit Rating: 0 out of 5 stars0 ratingsTest-Driven JavaScript Development Rating: 0 out of 5 stars0 ratingsLearning Behavior-driven Development with JavaScript Rating: 5 out of 5 stars5/5Angular JS for Beginners: Your Guide to Easily Learn Angular JS In 7 Days Rating: 2 out of 5 stars2/5Test-Driven Java Development Rating: 0 out of 5 stars0 ratingsNode.js By Example Rating: 2 out of 5 stars2/5Jasmine JavaScript Testing - Second Edition Rating: 0 out of 5 stars0 ratingsLearning Angular for .NET Developers Rating: 0 out of 5 stars0 ratingsClean Code in JavaScript: Develop reliable, maintainable, and robust JavaScript Rating: 5 out of 5 stars5/5Next.js Cookbook: Learn how to build scalable and high-performance apps from scratch (English Edition) Rating: 0 out of 5 stars0 ratingsJavaScript Mobile Application Development Rating: 0 out of 5 stars0 ratingsMastering JavaScript Rating: 4 out of 5 stars4/5JavaScript: Advanced Guide to Programming Code with JavaScript Rating: 0 out of 5 stars0 ratingsReactive State for Angular with NgRx: Learn to build Reactive Angular Applications using NgRx (English Edition) Rating: 0 out of 5 stars0 ratingsJavaScript for Modern Web Development: Building a Web Application Using HTML, CSS, and JavaScript Rating: 0 out of 5 stars0 ratingsObject Oriented Programming with Angular: Build and Deploy Your Web Application Using Angular with Ease ( English Edition) Rating: 0 out of 5 stars0 ratingsJavaScript for Gurus: Use JavaScript programming features, techniques and modules to solve everyday problems Rating: 0 out of 5 stars0 ratingsReact and React Native Rating: 4 out of 5 stars4/5
Software Development & Engineering For You
Hand Lettering on the iPad with Procreate: Ideas and Lessons for Modern and Vintage Lettering Rating: 4 out of 5 stars4/5Python For Dummies Rating: 4 out of 5 stars4/5Coding with AI For Dummies Rating: 1 out of 5 stars1/5The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers Rating: 4 out of 5 stars4/5Coding All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsLearn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Data Visualization: a successful design process Rating: 4 out of 5 stars4/5OneNote: The Ultimate Guide on How to Use Microsoft OneNote for Getting Things Done Rating: 1 out of 5 stars1/5Beginning Programming For Dummies Rating: 4 out of 5 stars4/5Level Up! The Guide to Great Video Game Design Rating: 4 out of 5 stars4/5SQL For Dummies Rating: 0 out of 5 stars0 ratingsAgile Practice Guide Rating: 4 out of 5 stars4/5Photoshop For Beginners: Learn Adobe Photoshop cs5 Basics With Tutorials Rating: 0 out of 5 stars0 ratingsThe Inmates Are Running the Asylum (Review and Analysis of Cooper's Book) Rating: 4 out of 5 stars4/5Adobe Illustrator CC For Dummies Rating: 5 out of 5 stars5/5Kodi Made Easy: Complete Beginners Step by Step Guide on How to Install Kodi on Amazon Firestick Rating: 0 out of 5 stars0 ratingsAndroid App Development For Dummies Rating: 0 out of 5 stars0 ratingsHow to Write Effective Emails at Work Rating: 4 out of 5 stars4/5Gray Hat Hacking the Ethical Hacker's Rating: 5 out of 5 stars5/5Fundamentals of Software Engineering: Designed to provide an insight into the software engineering concepts Rating: 0 out of 5 stars0 ratingsTeach Yourself VISUALLY iPhone 16 Rating: 0 out of 5 stars0 ratingsPYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/53D Printing For Dummies Rating: 4 out of 5 stars4/5Google SketchUp Pro 8 step by step Rating: 0 out of 5 stars0 ratingsGitLab Cookbook Rating: 0 out of 5 stars0 ratingsHow Do I Do That In InDesign? Rating: 5 out of 5 stars5/5
Reviews for Mastering Angular Reactive Forms
0 ratings0 reviews
Book preview
Mastering Angular Reactive Forms - Fanis Prodromou
CHAPTER 1
Getting Started with Reactive Forms
Introduction
Angular forms are a communication channel between a user and a system. We collect data through forms, and in most cases, we provide this data in a server using an API. Data is a valuable asset that should be accurate, and thus, working with Angular Reactive Forms is advantageous as we have all the needed tools to reassure data integrity.
In this chapter, we will learn what Angular Reactive Forms are and why we should master them. We will also learn all the Reactive Form Building Blocks: FormControl, FormGroup, and FormArray.
To fully follow this book, you should be reasonably familiar with the following:
Have the required skills to create Angular Components and Angular Modules
Basic knowledge of HTML5
Basic understanding of CSS3
Note: This book's code examples are available in the following GitHub repo separated into different git branches:
https://github.com/profanis/bpb-mastering-angular-reactive-forms
Structure
What are Reactive Forms?
Why use Reactive Forms?
Understanding the Building Blocks
Working with FormControl
Working with FormGroup
The formControlName directive
Nested FormGroup
The formGroupName directive
Using FormArray in a FormGroup
FormGroup Actions
The Form Model
The Form Builder
Objective
After reading this chapter, you will grasp the Reactive Forms fundamentals, and you will learn how to construct an Angular form. You will also learn to distinguish the various Reactive Form directives and use them per case to increase your overall performance.
What are Reactive Forms?
We have two options to construct Angular forms in the Angular framework: Template Driven Forms and Reactive Forms.
It's quite popular to use the latter in complex forms because it increases the developer's flexibility in terms of validations, unit testing, and splitting the code into smaller chunks.
Why use Reactive Forms?
Either developing a simple form or a very complex one, we need to be agile in changes, or better said, to have a maintainable and extensible form. Often the business requirements change, and we have to follow them, sometimes rapidly.
Having a form that does not respect the Separation of Concerns (SoC)¹ principle makes even the simplest change looks hard.
So, let's see what a form has to separate:
templating
form model
unit tests
validations
By "templating," we mean the actual form the end-user sees in the browser.
By "form model," we mean the model we create to apply a binding with the HTML template. Since this is an interesting topic, a separate section has been added at the end of this chapter.
The "unit tests" should be a developer's best friend. We are familiar with unit testing the business code in the Angular Services with Karma/Jasmine, Jest, or any other library. With Reactive Forms, we can test our forms without referring to the HTML but testing only the source code (component.ts).
The "validations can become complicated. An example would be:
An input element should have a minimum of three letters and a maximum of ten letters; it should contain at least one special character, and this validation should exist if another field has a certain value." Oh boy! Well, if we have the right tools, this is just a simple challenge to accomplish. And yes, with Reactive Forms, the validations can be fun.
Understanding the Building Blocks
There are three Building Blocks of the Reactive Form, and each one is responsible for handling the value and the validity. It's important to understand that we either set the value from the component.ts code or the value is set while the end-user types in the HTML form fields. It's OK not to understand this fully, but we will soon learn what value and validity mean.
The FormControl is a class that we map with the individual HTML input element
The FormGroup is a group of individual FormControl instances
The FormArray is an array of FormControl, FormGroup, or FormArray instances
The following figure depicts an example of the preceding:
Figure 1.1: Reactive Forms Building blocks
Working with FormControl
FormControl is one of the three fundamental building blocks of Reactive Forms. The other two are FormGroup and FormArray, which we will see in the next pages. In an HTML form, FormControl(s) are the HTML elements, such as input elements, checkboxes, radio buttons, and select menus.
Let's depict this with the following image:
Figure 1.2: A form with two FormControl items
In this figure, the "First Name and
Last Name" inputs are considered FormControl(s), but we first have to convert them to actual FormControl instances.
At first, and since Angular is modular, we have to import the corresponding module in our Single Page Application (SPA)².
Note: If a framework like Angular weren't modular and had everything loaded upfront by default, this would mean extra compilation and Tree Shaking time.
The corresponding module is ReactiveFormsModule, and we have to import it in every feature module³ we meant to use Reactive Forms. By importing this module, Angular injects the required functionality necessary to develop the forms. This functionality consists of several directives and various