Lesson 10 Forms
Lesson 10 Forms
Lesson 10—Forms
Forms are probably the most crucial aspect of your web application.
You can often get events by clicking on links or moving the mouse, but it’s through forms that you get
the majority of your rich data input from users.
Angular Form—Challenges
Form inputs are meant to modify data, Changes often need to be reflected
both on the page and the server. elsewhere on the page.
Users have a lot of leeway in what they You need to be able to test your forms
enter, so you need to validate values. without relying on DOM selectors.
and give objects to any way you like changes and respond
ControlGroup and Control extend from the same abstract class, which means that these
are in AbstractControl and refer to each other using this same abstract class.
AbstractControl
ControlGroup Control
AbstractControl gives you enough flexibility to match any HTML form structure.
These building blocks play an important role in the structure.
Angular Form—Control Class
First Name
Control
You can bind an existing Control to a DOM element using directives such
as NgFormControl and NgControlName.
Angular Form—Control Class (Contd.)
First Name
Control
• value
• touched
• untouched
• dirty
• pristine
• valid
• errors
Angular Form—ControlGroup
Most forms have more than one field, so you need a way to manage multiple Controls.
If you want to check the validity of your form, it’s cumbersome to iterate over an array of
Controls and check each Control for validity.
First Name
Control
Comment
ControlGroups solve this issue by providing a wrapper interface around a collection of Controls.
Angular Form—ControlGroup (Contd.)
First Name
Control
Control
Comment
Control
Angular Form—Validator
Validators are provided by the Validators module. The simplest validator is “required,” which
simply says that the designated field is required or the Control will be considered invalid.
02 Check the status of the validator in the view and take action accordingly
Angular Form—Observer
Observers allow you to watch your form for changes and respond accordingly.
In an imperative way, a variable is only changed when its state is mutated by
assigning a new or updated value.
With template-driven forms, you can essentially leave a component class empty until you
need to read/write values (such as submitting and setting initial or future data).
>_ training@localhost:~
@Component ({
selector: ‘signup - form’,
template: ‘
<form novalidate>…</form>
‘
})
export class signupFormComponent {
constructor()_ {}
}
Template-Driven Approach—Advantages and Disadvantages
Advantage Disadvantage
START LAB
Forms
Topic 3—Angular Validation
Angular Validation
Angular provides three out-of-the-box validators that can either be applied using
the “Control” Class, or using HTML properties.
For example: <input required> will automatically apply the required validator
• Required
• minLength
• maxLength
You can also write your own custom validators. Here is an example of a validator
that checks if the first character is not a number:
Interface ValidationResult {
[key:string] :boolean;
}
class UsernameValidator {
static startWithNumber (control: Control) :ValidationResult {
if ( control.value ! “” && !isNaN
(control.valuel.charAt(0)) ) {
}
return null;
}
}
Demo—Validation
START LAB
Forms
Topic 4—Model-Driven Approach
Model-Driven Approach
To promote some of those benefits in the context of forms, Angular uses a “model-
driven” or reactive technique of development.
Model-Driven Approach—Benefits
It's implied in a more concise HTML Template and provides more control
in your forms (but with more code on it).
>_
training@localhost:~
>_
training@localhost:~
@Component({
selector: ‘form’,
templateUrl: ‘./dev/shared/form.component.html’
})
Export class FormComponent {
form = new ControlGroup({
name: new Control(‘’, Validators.required);
});
onSubmit(){
console.log(this.form.value);
}
}
Model-Driven Approach—FormBuilder
This can be done by just instantiating the class, setting some values in the form
controls, and performing assertions against the form global valid state and the
validity state of each control.
The FormGroup and FormControl classes provide an API that allows you to build
UIs using a completely different programming style known as Functional Reactive
Programming.
Demo—Model-Driven Approach
START LAB
Key Takeaways
Angular aims to make the creation and validation of forms simple, intuitive,
and manageable.
Template-Driven approach was the typical way to build forms in the past,
and it is still useful for building very simple forms in Angular.
Angular comes with a brand new approach to forms that makes it easier to
construct and apply validation to them.
a. required
b. minlength
c. maxlength
d. notfound
QUIZ
Which of the following is NOT a built-in validator in Angular?
2
a. required
b. minlength
c. maxlength
d. notfound
a. True
b. False
QUIZ
Angular forms Control follows AbstractControl pattern.
3
a. True
b. False