This repository has been archived by the owner on Nov 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
angular-validation.html
127 lines (100 loc) · 3.09 KB
/
angular-validation.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<!DOCTYPE html>
<html>
<head>
<title>Demo</title>
<style>
.valdr-message {
display: inline;
background: red;
}
input.ng-invalid {
background: red;
}
.form-group.ng-invalid label {
color: red;
}
</style>
</head>
<body ng-app="demoApp">
<div ng-controller="TestController" ng-class="{ 'form-submitted': formSubmitted }">
<h1>AngularJS validation messages</h1>
<p>This example shows that by using valdrMessage it is possible to register validation messages
for both AngularJS built-in and valdr validators.</p>
<p> The last name field has AngularJS and valdr validators, while the age field is of type number and therefore
is automatically validated by the AngularJS number validator.</p>
<form name="demoForm" novalidate>
<div valdr-type="Person">
<h4>Person</h4>
<div valdr-form-group>
<label for="lastName">Last Name</label>
<input type="text"
id="lastName"
name="lastName"
required
ng-minlength="2"
ng-model="person.lastName">
</div>
<div valdr-form-group>
<label for="age">Age</label>
<input type="number"
id="age"
name="age"
ng-model="person.age">
</div>
</div>
</form>
<pre>demoForm.$valid = {{demoForm.$valid}}</pre>
<h3>constraints:</h3>
<pre>{{ constraints | json }}</pre>
</div>
<script src="/bower_components/angular/angular.js"></script>
<script src="/bower_components/angular-translate/angular-translate.js"></script>
<script src="../js/valdr.js"></script>
<script src="../js/valdr-message.js"></script>
<script src="http://localhost:35729/livereload.js"></script>
<script>
var demoApp = angular.module('demoApp', ['valdr', 'pascalprecht.translate']);
demoApp.config(function(valdrProvider) {
valdrProvider.addConstraints({
'Person': {
'lastName': {
'size': {
'max': 4,
'message': 'Last name max is 4 characters.'
}
}
}
});
});
demoApp.config(function (valdrMessageProvider, $translateProvider) {
$translateProvider.translations('en', {
'message.required': '{{fieldName}} is required.',
'message.number': '{{fieldName}} must be a number.',
'lastname.specific': '{{fieldName}} min is 2 characters.',
'Person.lastName': 'Last name',
'Person.age': 'Age'
});
$translateProvider.preferredLanguage('en');
valdrMessageProvider.addMessages({
'required': 'message.required',
'number': 'message.number',
'Person.lastName.minlength': 'lastname.specific'
});
});
demoApp.run(function (valdrMessage) {
valdrMessage.angularMessagesEnabled = true;
});
demoApp.controller('TestController', function ($scope, valdr) {
$scope.person = {};
$scope.genders = [
{ label: 'Male', value: 1 },
{ label: 'Female', value: 2 }
];
$scope.constraints = valdr.getConstraints();
$scope.showAllErrors = function () {
$scope.formSubmitted = true;
}
});
</script>
</body>
</html>