From a1c5f2b4f043fcbc0a5af3012b074e4cb02fc21a Mon Sep 17 00:00:00 2001 From: Clay Anderson Date: Fri, 14 Nov 2014 15:22:56 -0700 Subject: [PATCH] docs(guide/forms): enhanced form examples to utilize $touched The "Binding to form and control state" example now makes use of control states that were introduced in 1.3. For example, users are now informed of validation requirements upon clicking 'Save'. Closes #10066 --- docs/content/guide/forms.ngdoc | 71 ++++++++++++++++++++-------------- 1 file changed, 41 insertions(+), 30 deletions(-) diff --git a/docs/content/guide/forms.ngdoc b/docs/content/guide/forms.ngdoc index 370ffbe3..e25c12fd 100644 --- a/docs/content/guide/forms.ngdoc +++ b/docs/content/guide/forms.ngdoc @@ -27,8 +27,8 @@ for other directives to augment its behavior. E-mail:
Gender: male female
- - + +
form = {{user | json}}
master = {{master | json}}
@@ -77,29 +77,29 @@ To allow styling of form as well as controls, `ngModel` adds 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. +with red background only after the input is blurred (loses focus). +This ensures that the user is not distracted with an error until after interacting with the control, +and failing to satisfy its validity.
- Name: -
+ Name:
E-mail:
Gender: male female
- - + +
@@ -140,34 +140,45 @@ 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` +- Custom error message displayed after the user interacted with a control (i.e. when `$touched` is set) +- Custom error message displayed upon submitting the form (`$submitted` is set), even if the user +didn't interact with a control +
Name: -
+ +
+
+
Tell us your name.
+
+ E-mail: -
-
Invalid: + +
+
Tell us your email. This is not a valid email.
- Gender: male - female
+ Gender: + male + female +
+ - - I agree:
-
Please agree and sign.
+ I agree: + +
+
+
Please agree and sign.
+
- - + +
@@ -181,14 +192,14 @@ This allows us to extend the above example with these features: $scope.master = angular.copy(user); }; - $scope.reset = function() { + $scope.reset = function(form) { + if (form) { + form.$setPristine(); + form.$setUntouched(); + } $scope.user = angular.copy($scope.master); }; - $scope.isUnchanged = function(user) { - return angular.equals(user, $scope.master); - }; - $scope.reset(); }]);