Skip to content

Commit af5ea19

Browse files
committed
unique field event #22
Thanks @gilad1987
1 parent 23b5bbe commit af5ea19

File tree

5 files changed

+42
-9
lines changed

5 files changed

+42
-9
lines changed

dist/angular-validation.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@
190190

191191
for (var k in form) {
192192
if (form[k].hasOwnProperty('$dirty')) {
193-
$scope.$broadcast(k + 'submit', idx++);
193+
$scope.$broadcast(k + 'submit-' + form[k].validationId, idx++);
194194
}
195195
}
196196

@@ -229,7 +229,7 @@
229229
this.reset = function (form) {
230230
for (var k in form) {
231231
if (form[k].hasOwnProperty('$dirty')) {
232-
$scope.$broadcast(k + 'reset');
232+
$scope.$broadcast(k + 'reset-' + form[k].validationId);
233233
}
234234
}
235235
};
@@ -366,6 +366,17 @@
366366
};
367367

368368

369+
/**
370+
* generate unique guid
371+
*/
372+
var s4 = function () {
373+
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
374+
};
375+
var guid = function () {
376+
return (s4() + s4() + s4() + s4());
377+
};
378+
379+
369380
return {
370381
restrict: 'A',
371382
require: 'ngModel',
@@ -394,6 +405,11 @@
394405
*/
395406
var validator = attrs.validator.split(',');
396407

408+
/**
409+
* guid use
410+
*/
411+
var uid = ctrl.validationId = guid();
412+
397413
/**
398414
* Valid/Invalid Message
399415
*/
@@ -407,7 +423,7 @@
407423
/**
408424
* Reset the validation for specific form
409425
*/
410-
scope.$on(ctrl.$name + 'reset', function () {
426+
scope.$on(ctrl.$name + 'reset-' + uid, function () {
411427

412428
/**
413429
* clear scope.$watch here
@@ -433,7 +449,7 @@
433449
/**
434450
* Click submit form, check the validity when submit
435451
*/
436-
scope.$on(ctrl.$name + 'submit', function (event, index) {
452+
scope.$on(ctrl.$name + 'submit-' + uid, function (event, index) {
437453
var value = element[0].value,
438454
isValid = false;
439455

dist/angular-validation.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ <h1>Angular Validation.
2929
<div class="row">
3030
<div class="small-12 medium-12 large-6 columns">
3131
<form name="Form" id="form1">
32+
{{ Form }}
3233
<fieldset>
3334
<legend>Form ($watch)</legend>
3435

src/directive.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,17 @@
103103
};
104104

105105

106+
/**
107+
* generate unique guid
108+
*/
109+
var s4 = function () {
110+
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
111+
};
112+
var guid = function () {
113+
return (s4() + s4() + s4() + s4());
114+
};
115+
116+
106117
return {
107118
restrict: 'A',
108119
require: 'ngModel',
@@ -131,6 +142,11 @@
131142
*/
132143
var validator = attrs.validator.split(',');
133144

145+
/**
146+
* guid use
147+
*/
148+
var uid = ctrl.validationId = guid();
149+
134150
/**
135151
* Valid/Invalid Message
136152
*/
@@ -144,7 +160,7 @@
144160
/**
145161
* Reset the validation for specific form
146162
*/
147-
scope.$on(ctrl.$name + 'reset', function () {
163+
scope.$on(ctrl.$name + 'reset-' + uid, function () {
148164

149165
/**
150166
* clear scope.$watch here
@@ -170,7 +186,7 @@
170186
/**
171187
* Click submit form, check the validity when submit
172188
*/
173-
scope.$on(ctrl.$name + 'submit', function (event, index) {
189+
scope.$on(ctrl.$name + 'submit-' + uid, function (event, index) {
174190
var value = element[0].value,
175191
isValid = false;
176192

src/provider.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@
187187

188188
for (var k in form) {
189189
if (form[k].hasOwnProperty('$dirty')) {
190-
$scope.$broadcast(k + 'submit', idx++);
190+
$scope.$broadcast(k + 'submit-' + form[k].validationId, idx++);
191191
}
192192
}
193193

@@ -226,7 +226,7 @@
226226
this.reset = function (form) {
227227
for (var k in form) {
228228
if (form[k].hasOwnProperty('$dirty')) {
229-
$scope.$broadcast(k + 'reset');
229+
$scope.$broadcast(k + 'reset-' + form[k].validationId);
230230
}
231231
}
232232
};

0 commit comments

Comments
 (0)