forked from json-schema-form/angular-schema-form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom-validators.html
123 lines (109 loc) · 3.98 KB
/
custom-validators.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
<!DOCTYPE html>
<html>
<head>
<title>Custom validators, async validators etc</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">
</head>
<body ng-app="test" class="container" ng-controller="TestCtrl">
<h3>Demo of custom validators, async validators and parsers</h3>
<span>Check the source</span>
<form name="theForm">
<div sf-schema="schema" sf-form="form" sf-model="model"></div>
<div>
The form is <em ng-show="theForm.$pristine">pristine</em><em ng-show="theForm.$dirty">dirty</em>
and <em ng-show="theForm.$valid">valid</em><em ng-show="!theForm.$valid">invalid</em>.
</div>
<div>{{prettyModel}}</div>
</form>
<script type="text/javascript" src="../bower_components/tv4/tv4.js"></script>
<script type="text/javascript" src="../bower_components/angular/angular.min.js"></script>
<script type="text/javascript" src="../bower_components/angular-sanitize/angular-sanitize.min.js"></script>
<script type="text/javascript" src="../bower_components/objectpath/lib/ObjectPath.js"></script>
<script type="text/javascript" src="../dist/schema-form.js"></script>
<script type="text/javascript" src="../dist/bootstrap-decorator.min.js"></script>
<script>
angular.module('test', ['schemaForm']).controller('TestCtrl', function($scope, $q, $timeout) {
$scope.schema = {
"type": "object",
"title": "Comment",
"properties": {
"name": {
"title": "Name",
"type": "string"
},
"email": {
"title": "Email",
"type": "string",
"pattern": "^\\S+@\\S+$",
"description": "Email will be used for evil."
},
"comment": {
"title": "Comment",
"type": "string"
}
},
"required": ["name","email","comment"]
};
$scope.form = [
{
key: 'name',
placeholder: 'Anything but "Bob"',
$asyncValidators: {
'async': function(name) {
var deferred = $q.defer();
$timeout(function(){
if (angular.isString(name) && name.toLowerCase().indexOf('bob') !== -1) {
deferred.reject();
} else {
deferred.resolve();
}
}, 500);
return deferred.promise;
}
},
validationMessage: {
'async': "Wooohoo thats not an OK name!"
}
},
{
key: 'email',
placeholder: 'Not MY email',
ngModel: function(ngModel) {
ngModel.$validators.myMail = function(value) {
return value !== '[email protected]';
};
},
validationMessage: {
'myMail': "Thats my mail!"
}
},
{
"key": "comment",
"type": "textarea",
"placeholder": "Make a comment, write 'damn' and check the model",
$parsers: [
function(value) {
if (value && value.replace) {
return value.replace(/(damn|fuck|apple)/,'#!@%&');
}
return value;
}
]
},
{
"type": "submit",
"style": "btn-info",
"title": "OK"
}
];
$scope.model = {};
$scope.$watch('model', function(value){
if (value) {
$scope.prettyModel = JSON.stringify(value, undefined, 2);
}
}, true);
});
</script>
</body>
</html>