Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Enhanced form examples to utilize $touched #10066

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 43 additions & 34 deletions docs/content/guide/forms.ngdoc
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ for other directives to augment its behavior.
E-mail: <input type="email" ng-model="user.email" /><br />
Gender: <input type="radio" ng-model="user.gender" value="male" />male
<input type="radio" ng-model="user.gender" value="female" />female<br />
<button ng-click="reset()">RESET</button>
<button ng-click="update(user)">SAVE</button>
<input type="button" ng-click="reset()" value="Reset" />
<input type="submit" ng-click="update(user)" value="Save" />
</form>
<pre>form = {{user | json}}</pre>
<pre>master = {{master | json}}</pre>
Expand Down Expand Up @@ -77,29 +77,28 @@ 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 red background only when they are touched. This ensures that the user is not distracted
with an error until after interacting with the control, and failing to satisfy its validity.

<example module="formExample">
<file name="index.html">
<div ng-controller="ExampleController">
<form novalidate class="css-form">
Name:
<input type="text" ng-model="user.name" required /><br />
Name: <input type="text" ng-model="user.name" required /><br />
E-mail: <input type="email" ng-model="user.email" required /><br />
Gender: <input type="radio" ng-model="user.gender" value="male" />male
<input type="radio" ng-model="user.gender" value="female" />female<br />
<button ng-click="reset()">RESET</button>
<button ng-click="update(user)">SAVE</button>
<input type="button" ng-click="reset()" value="Reset" />
<input type="submit" ng-click="update(user)" value="Save" />
</form>
</div>

<style type="text/css">
.css-form input.ng-invalid.ng-dirty {
.css-form input.ng-invalid.ng-touched {
background-color: #FA787E;
}

.css-form input.ng-valid.ng-dirty {
.css-form input.ng-valid.ng-touched {
background-color: #78FA89;
}
</style>
Expand Down Expand Up @@ -140,34 +139,44 @@ 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 interacting with a control (i.e. `$touched`)
- Custom error message displayed upon submitting the form even if user didn't interact with a control (i.e. `$submitted`)


<example module="formExample">
<file name="index.html">
<div ng-controller="ExampleController">
<form name="form" class="css-form" novalidate>
Name:
<input type="text" ng-model="user.name" name="uName" required /><br />
<input type="text" ng-model="user.name" name="uName" required="" />
<br />
<div ng-show="form.$submitted || form.uName.$touched">
<div ng-show="form.uName.$error.required">Tell us your name.</div>
</div>

E-mail:
<input type="email" ng-model="user.email" name="uEmail" required/><br />
<div ng-show="form.uEmail.$dirty && form.uEmail.$invalid">Invalid:
<input type="email" ng-model="user.email" name="uEmail" required="" />
<br />
<div ng-show="form.$submitted || form.uEmail.$touched">
<span ng-show="form.uEmail.$error.required">Tell us your email.</span>
<span ng-show="form.uEmail.$error.email">This is not a valid email.</span>
</div>

Gender: <input type="radio" ng-model="user.gender" value="male" />male
<input type="radio" ng-model="user.gender" value="female" />female<br />

<input type="checkbox" ng-model="user.agree" name="userAgree" required />
I agree: <input ng-show="user.agree" type="text" ng-model="user.agreeSign"
required /><br />
<div ng-show="!user.agree || !user.agreeSign">Please agree and sign.</div>

<button ng-click="reset()" ng-disabled="isUnchanged(user)">RESET</button>
<button ng-click="update(user)"
ng-disabled="form.$invalid || isUnchanged(user)">SAVE</button>
Gender:
<input type="radio" ng-model="user.gender" value="male" />male
<input type="radio" ng-model="user.gender" value="female" />female
<br />
<input type="checkbox" ng-model="user.agree" name="userAgree" required="" />

I agree:
<input ng-show="user.agree" type="text" ng-model="user.agreeSign" required="" />
<br />
<div ng-show="form.$submitted || form.userAgree.$touched">
<div ng-show="!user.agree || !user.agreeSign">Please agree and sign.</div>
</div>

<input type="button" ng-click="reset(form)" value="Reset" />
<input type="submit" ng-click="update(user)" value="Save" />
</form>
</div>
</file>
Expand All @@ -176,19 +185,19 @@ This allows us to extend the above example with these features:
angular.module('formExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.master = {};

$scope.update = function(user) {
$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();
}]);
</file>
Expand Down