@ngdoc overview @name Forms @description 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 {@link ng.directive:ngModel 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 {@link ng.directive:ngModel.NgModelController API} for other directives to augment its behavior.
Name:
E-mail:
Gender: male female
form = {{user | json}}
master = {{master | json}}
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: - `ng-valid` - `ng-invalid` - `ng-pristine` - `ng-dirty` 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.
Name:
E-mail:
Gender: male female
# Binding to form and control state A form is an instance of {@link ng.directive:form.FormController FormController}. The form instance can optionally be published into the scope using the `name` attribute. Similarly, an input control that has the {@link ng.directive:ngModel ngModel} directive holds an instance of {@link ng.directive:ngModel.NgModelController NgModelController}. Such a control instance can be published as a property of the form instance using the `name` attribute on the input control. The name attribute specifies the name of the property on the form instance. 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: - RESET button is enabled only if form has some changes - SAVE button is enabled only if form has some changes and is valid - custom error messages for `user.email` and `user.agree`
Name:
E-mail:
Invalid: Tell us your email. This is not a valid email.
Gender: male female
I agree:
Please agree and sign.
# Custom Validation Angular provides basic implementation for most common html5 {@link ng.directive:input input} types: ({@link ng.directive:input.text text}, {@link ng.directive:input.number number}, {@link ng.directive:input.url url}, {@link ng.directive:input.email email}, {@link ng.directive:input.radio radio}, {@link ng.directive:input.checkbox 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` {@link ng.directive:ngModel.NgModelController 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: * **Model to View update** - Whenever the bound model changes, all functions in {@link ng.directive:ngModel.NgModelController#properties_$formatters NgModelController#$formatters} array are pipe-lined, so that each of these functions has an opportunity to format the value and change validity state of the form control through {@link ng.directive:ngModel.NgModelController#methods_$setValidity NgModelController#$setValidity}. * **View to Model update** - In a similar way, whenever a user interacts with a control it calls {@link ng.directive:ngModel.NgModelController#methods_$setViewValue NgModelController#$setViewValue}. This in turn pipelines all functions in the {@link ng.directive:ngModel.NgModelController#properties_$parsers NgModelController#$parsers} array, so that each of these functions has an opportunity to convert the value and change validity state of the form control through {@link ng.directive:ngModel.NgModelController#methods_$setValidity NgModelController#$setValidity}. In the following example we create two directives. * The first one is `integer` and it validates whether the input is a valid integer. For example `1.23` is an invalid value, since it contains a fraction. Note that we unshift the array instead of pushing. This is because we want to be first parser and consume the control string value, as we need to execute the validation function before a conversion to number occurs. * The second directive is a `smart-float`. It parses both `1.2` and `1,2` into a valid float number `1.2`. Note that we can't use input type `number` here as HTML5 browsers would not allow the user to type what it would consider an invalid number such as `1,2`.
Size (integer 0 - 10): {{size}}
This is not valid integer! The value must be in range 0 to 10!
Length (float): {{length}}
This is not a valid float number!
# Implementing custom form controls (using `ngModel`) Angular implements all of the basic HTML form controls ({@link ng.directive:input input}, {@link ng.directive:select select}, {@link ng.directive:textarea 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: - implement `$render` method, which is responsible for rendering the data after it passed the {@link api/ng.directive:ngModel.NgModelController#properties_$formatters NgModelController#$formatters}, - call `$setViewValue` method, whenever the user interacts with the control and model needs to be updated. This is usually done inside a DOM Event listener. See {@link guide/directive $compileProvider.directive} for more info. The following example shows how to add two-way data-binding to contentEditable elements.
Some
model = {{content}}