Professional Documents
Culture Documents
Produced by
• What is Custom Form Validation?
• Validator Functions and Directives
• Getting Started with Angular
• Demo: Setting Up the Angular Starter Application
• Validator Functions
• Demo: Creating a Custom Telephone Validator Function
• Applying Validators with a Directive
• Demo: Using a Directive to Apply the Telephone Validator
• Creating Custom Asynchronous Validators
• Demo: Validate a Book Id on the Server
• Applying a Custom Asynchronous Validator with a Directive
• Demo: Apply the Book Id Validator with a Directive
• Validating a Model Group
• Demo: Geocoding an Address
• Angular Forms and the built-in validators are very helpful for creating
forms and validating them
• In many situations though, more customized validation is needed
• Angular Forms support the creation of custom validators
• Custom validators can work synchronously or asynchronously
• Synchronous validators use logic and data already loaded in the client to
perform validations
• Asynchronous validators can access network resources to validate data
on the server
• Custom validators are written as functions
• Custom validator functions can be wrapped in directives and applied as
directives to template forms
• Custom validators can be applied on controls and form groups
• Custom validators can utilize Angular services to help validate controls
• Angular applications require some initial project configuration to get up
and running
• There are many ways to get started
• Angular CLI
• Numerous Starter Projects
• From Scratch
• This course will use a starter project configured with WebPack 2
• The WebPack development server will serve the web pages, and
WebPack will bundle the application files
• The initial starter project can be downloaded from GitHub
• Git is not required, simply fire up a web browser, and visit the following
link
• https://github.com/training4developers/mastering-angular-starter
• In the demonstration we will download, configure and fire up the project
Demo
Setting Up the Angular Starter Application
Validator Functions
• The return object will be mixed into the errors property of the form
control object
• The form control's errors object can then be used to update the UI with
information about what failed
• If additional information is provided on the object returned from the
validator object, that information is available on the errors object
through the property set by the validator
• A common use for additional information is identify specific errors in a
complex validation
Validator Functions
• A failed result from the validator object will result in the control, group
or form being updated with the ng-invalid class
• Once the validator passes (after the user corrects any errors), the ng-
invalid class will be removed and replaced with ng-valid
Validator Functions
• When working with validator functions, they are only useful with
Reactive (aka Model-Driven) forms
• To apply a validator function to a template form control, group or form,
it must be wrapped in a directive (covered in the next section)
• Final note, these validator functions are known as synchronous validator
functions, in the second half of the course, asynchronous validators will
be covered
Demo
Creating a Custom Telephone Validator Function
• To use custom validator functions with template forms, they must be
wrapped in a directive
• To implement the directive, the custom validator needs to be added to
the NG_VALIDATORS multi-provider, and a selector needs to configured
• Multi-providers allow multiple services to be injected for a single
injection token
• For validators, there are two multi-providers, NG_VALIDATORS and
NG_ASYNC_VALIDATORS
• The multi-providers are an array of validators
• In addition to the multi-provider configuration, a selector needs to be
specified
• Some examples include:
• [type=tel][ngModel] – apply the validator to elements with NgModel
and of type telephone
• [ssn][formControl] – apply the validator to elements with the
formControl and the ssn attributes (yes, validator directives can be
used with reactive forms)
• form[address] – apply the address validator to forms with an address
attribute
Demo
Using a Directive to Apply the Telephone Validator
Creating Custom Asynchronous Validators