-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathdemo.coffee
125 lines (110 loc) · 3.63 KB
/
demo.coffee
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
angular.module 'app', ['validator', 'validator.rules']
# ----------------------------------------
# config
# ----------------------------------------
.config ($validatorProvider) ->
# backendWatch
$validatorProvider.register 'backendWatch',
invoke: 'watch'
validator: (value, scope, element, attrs, $injector) ->
$http = $injector.get '$http'
$http.get 'example/data.json'
.then (response) ->
if response and response.data
not (value in (x.name for x in response.data))
else
no
error: "do not use 'Kelp' or 'x'"
$validatorProvider.register 'backendSubmit',
validator: (value, scope, element, attrs, $injector) ->
$http = $injector.get '$http'
$http.get 'example/data.json'
.then (response) ->
if response and response.data
not (value in (x.name for x in response.data))
else
no
error: "do not use 'Kelp' or 'x'"
$validatorProvider.register 'backendBlur',
invoke: 'blur'
validator: (value, scope, element, attrs, $injector) ->
$http = $injector.get '$http'
$http.get 'example/data.json'
.then (response) ->
if response and response.data
not (value in (x.name for x in response.data))
else
no
error: "do not use 'Kelp' or 'x'"
# submit - required
$validatorProvider.register 'requiredSubmit',
validator: /^.+$/
error: 'This field is required.'
# blur - required
$validatorProvider.register 'requiredBlur',
invoke: 'blur'
validator: /^.+$/
error: 'This field is required.'
$validatorProvider.register 'numberSubmit',
validator: /^[-+]?[0-9]*[\.]?[0-9]*$/
error: 'This field should be number.'
# watch - custom less than xx
$validatorProvider.register 'customLess',
invoke: 'watch'
validator: (value, scope) -> value < scope.formWatch.number
error: 'It should less than number 1.'
# ----------------------------------------
# run
# ----------------------------------------
.run ($validator) ->
$validator.register 'requiredRun',
invoke: 'watch'
validator: /^.+$/
error: 'This field is requrired.'
# ----------------------------------------
# controller
# ----------------------------------------
.controller 'DemoController', ($scope, $validator) ->
$scope.formWatch =
required: ''
regexp: ''
requiredRun: ''
number: 100
number2: ''
http: ''
$scope.formSubmit =
required: ''
regexp: ''
number: ''
http: ''
# the submit function
submit: ->
$validator.validate $scope, 'formSubmit'
.success ->
# validated success
console.log 'success'
.error ->
# validated error
console.log 'error'
.then ->
console.log 'then'
reset: ->
$validator.reset $scope, 'formSubmit'
$scope.formBlur =
required: ''
email: ''
url: ''
regexp: ''
http: ''
$scope.formRepeat =
model: [
value: 'a'
, value: 100,
value: ''
]
submit: ->
$validator.validate $scope, 'formRepeat'
.success -> console.log 'success'
.error -> console.log 'error'
reset: ->
$validator.reset $scope, 'formRepeat'