include ../_util-fns a#top :marked Improve overall data quality by validating user input for accuracy and completeness. This cookbook shows how to validate user input in the UI and display useful validation messages using first the template-driven forms and then the reactive forms approach. .l-sub-section :marked Read more about these choices in the [Forms](../guide/forms.html) and the [Reactive Forms](../guide/reactive-forms.html) guides. a#toc :marked ## Contents * [Simple template-driven forms](#template1) * [Template-driven forms with validation messages in code](#template2) - [Component Class](#component-class) - [The benefits of messages in code](#improvement) - [`FormModule` and template-driven forms](#formmodule) * [Reactive forms with validation in code](#reactive) - [Switch to the `ReactiveFormsModule`](#reactive-forms-module) - [Component template](#reactive-component-template) - [Component class](#reactive-component-class) - [`FormBuilder` declaration](#formbuilder) - [Committing hero value changes](#committing-changes) * [Custom validation](#custom-validation) - [Custom validation directive](#custom-validation-directive) * [Testing considerations](#testing) a#live-example :marked **Try the live example to see and download the full cookbook source code.** live-example(name="cb-form-validation" embedded img="cookbooks/form-validation/plunker.png") .l-main-section a#template1 :marked ## Simple template-driven forms In the template-driven approach, you arrange [form elements](https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms_in_HTML) in the component's template. You add Angular form directives (mostly directives beginning `ng...`) to help Angular construct a corresponding internal control model that implements form functionality. In template-drive forms, the control model is _implicit_ in the template. To validate user input, you add [HTML validation attributes](https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML5/Constraint_validation) to the elements. Angular interprets those as well, adding validator functions to the control model. Angular exposes information about the state of the controls including whether the user has "touched" the control or made changes and if the control values are valid. In this first template validation example, notice the HTML that reads the control state and updates the display appropriately. Here's an excerpt from the template HTML for a single input control bound to the hero name: +makeExample('cb-form-validation/ts/src/app/template/hero-form-template1.component.html','name-with-error-msg','template/hero-form-template1.component.html (Hero name)')(format='.') :marked Note the following: - The `` element carries the HTML validation attributes: `required`, `minlength`, and `maxlength`. - The `name` attribute of the input is set to `"name"` so Angular can track this input element and associate it with an Angular form control called `name` in its internal control model. - The `[(ngModel)]` directive allows two-way data binding between the input box to the `hero.name` property. - The template variable (`#name`) has the value `"ngModel"` (always `ngModel`). This gives you a reference to the Angular `NgModel` directive associated with this control that you can use _in the template_ to check for control states such as `valid` and `dirty`. - The `*ngIf` on the `