Improve this doc

Controls (input, select, textarea) are ways for a user to enter data. A Form is a collection of controls for the purpose of grouping related controls together.

Form and controls provide validation services, so that the user can be notified of invalid input. This provides a better user experience, because the user gets instant feedback on how to correct the error. Keep in mind that while client-side validation plays an important role in providing good user experience, it can easily be circumvented and thus can not be trusted. Server-side validation is still necessary for a secure application.

Simple form

The key directive in understanding two-way data-binding is ngModel. The ngModel directive provides the two-way data-binding by synchronizing the model to the view, as well as view to the model. In addition it provides an API for other directives to augment its behavior.

Source





Demo

Note that novalidate is used to disable browser's native form validation.

Using CSS classes

To allow styling of form as well as controls, ngModel add these CSS classes:

The following example uses the CSS to display validity of each form control. In the example both user.name and user.email are required, but are rendered with red background only when they are dirty. This ensures that the user is not distracted with an error until after interacting with the control, and failing to satisfy its validity.

Source





Demo

Binding to form and control state

A form is an instance of FormController. The form instance can optionally be published into the scope using the name attribute. Similarly control is an instance of NgModelController. The control instance can similarly be published into the form instance using the name attribute. This implies that the internal state of both the form and the control is available for binding in the view using the standard binding primitives.

This allows us to extend the above example with these features:

Source





Demo

Custom Validation

Angular provides basic implementation for most common html5 input types: (text, number, url, email, radio, checkbox), as well as some directives for validation (required, pattern, minlength, maxlength, min, max).

Defining your own validator can be done by defining your own directive which adds a custom validation function to the ngModel controller. To get a hold of the controller the directive specifies a dependency as shown in the example below. The validation can occur in two places:

In the following example we create two directives.

Source





Demo

Implementing custom form controls (using ngModel)

Angular implements all of the basic HTML form controls (input, select, textarea), which should be sufficient for most cases. However, if you need more flexibility, you can write your own form control as a directive.

In order for custom control to work with ngModel and to achieve two-way data-binding it needs to:

See $compileProvider.directive for more info.

The following example shows how to add two-way data-binding to contentEditable elements.

Source





Demo