Skip to content

Commit e16c592

Browse files
committed
Allow validate/reset only one scope model
1 parent 343f578 commit e16c592

File tree

4 files changed

+53
-23
lines changed

4 files changed

+53
-23
lines changed

dist/angular-validation.js

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,13 @@
183183
return deferred.promise;
184184
}
185185

186-
for (var k in form) {
187-
if (form[k].hasOwnProperty('$dirty')) {
188-
$scope.$broadcast(k + 'submit-' + form[k].validationId, idx++);
186+
if (form.validationId) {
187+
$scope.$broadcast(form.$name + 'submit-' + form.validationId, idx++);
188+
} else {
189+
for (var k in form) {
190+
if (form[k] && form[k].hasOwnProperty('$dirty')) {
191+
$scope.$broadcast(k + 'submit-' + form[k].validationId, idx++);
192+
}
189193
}
190194
}
191195

@@ -221,9 +225,19 @@
221225
* @param form
222226
*/
223227
this.reset = function (form) {
224-
for (var k in form) {
225-
if (form[k].hasOwnProperty('$dirty')) {
226-
$scope.$broadcast(k + 'reset-' + form[k].validationId);
228+
229+
if (form === undefined) {
230+
console.error('This is not a regular Form name scope');
231+
return;
232+
}
233+
234+
if (form.validationId) {
235+
$scope.$broadcast(form.$name + 'reset-' + form.validationId);
236+
} else {
237+
for (var k in form) {
238+
if (form[k].hasOwnProperty('$dirty')) {
239+
$scope.$broadcast(k + 'reset-' + form[k].validationId);
240+
}
227241
}
228242
}
229243
};
@@ -552,13 +566,13 @@
552566
priority: 1, // execute before ng-click (0)
553567
terminal: true,
554568
link: function postLink(scope, element, attrs) {
555-
var form = attrs.validationSubmit;
569+
var form = $parse(attrs.validationSubmit)(scope);
556570

557571
$timeout(function () {
558572
element.on('click', function (e) {
559573
e.preventDefault();
560574

561-
$validationProvider.validate(scope[form])
575+
$validationProvider.validate(form)
562576
.success(function () {
563577
$parse(attrs.ngClick)(scope);
564578
});
@@ -572,16 +586,17 @@
572586
.directive('validationReset', ['$injector', function ($injector) {
573587

574588
var $validationProvider = $injector.get('$validation'),
575-
$timeout = $injector.get('$timeout');
589+
$timeout = $injector.get('$timeout'),
590+
$parse = $injector.get('$parse');
576591

577592
return {
578593
link: function postLink(scope, element, attrs) {
579-
var form = attrs.validationReset;
594+
var form = $parse(attrs.validationReset)(scope);
580595

581596
$timeout(function () {
582597
element.on('click', function (e) {
583598
e.preventDefault();
584-
$validationProvider.reset(scope[form]);
599+
$validationProvider.reset(form);
585600
});
586601
});
587602

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.

src/directive.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -295,13 +295,13 @@
295295
priority: 1, // execute before ng-click (0)
296296
terminal: true,
297297
link: function postLink(scope, element, attrs) {
298-
var form = attrs.validationSubmit;
298+
var form = $parse(attrs.validationSubmit)(scope);
299299

300300
$timeout(function () {
301301
element.on('click', function (e) {
302302
e.preventDefault();
303303

304-
$validationProvider.validate(scope[form])
304+
$validationProvider.validate(form)
305305
.success(function () {
306306
$parse(attrs.ngClick)(scope);
307307
});
@@ -315,16 +315,17 @@
315315
.directive('validationReset', ['$injector', function ($injector) {
316316

317317
var $validationProvider = $injector.get('$validation'),
318-
$timeout = $injector.get('$timeout');
318+
$timeout = $injector.get('$timeout'),
319+
$parse = $injector.get('$parse');
319320

320321
return {
321322
link: function postLink(scope, element, attrs) {
322-
var form = attrs.validationReset;
323+
var form = $parse(attrs.validationReset)(scope);
323324

324325
$timeout(function () {
325326
element.on('click', function (e) {
326327
e.preventDefault();
327-
$validationProvider.reset(scope[form]);
328+
$validationProvider.reset(form);
328329
});
329330
});
330331

src/provider.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,13 @@
180180
return deferred.promise;
181181
}
182182

183-
for (var k in form) {
184-
if (form[k].hasOwnProperty('$dirty')) {
185-
$scope.$broadcast(k + 'submit-' + form[k].validationId, idx++);
183+
if (form.validationId) {
184+
$scope.$broadcast(form.$name + 'submit-' + form.validationId, idx++);
185+
} else {
186+
for (var k in form) {
187+
if (form[k] && form[k].hasOwnProperty('$dirty')) {
188+
$scope.$broadcast(k + 'submit-' + form[k].validationId, idx++);
189+
}
186190
}
187191
}
188192

@@ -218,9 +222,19 @@
218222
* @param form
219223
*/
220224
this.reset = function (form) {
221-
for (var k in form) {
222-
if (form[k].hasOwnProperty('$dirty')) {
223-
$scope.$broadcast(k + 'reset-' + form[k].validationId);
225+
226+
if (form === undefined) {
227+
console.error('This is not a regular Form name scope');
228+
return;
229+
}
230+
231+
if (form.validationId) {
232+
$scope.$broadcast(form.$name + 'reset-' + form.validationId);
233+
} else {
234+
for (var k in form) {
235+
if (form[k].hasOwnProperty('$dirty')) {
236+
$scope.$broadcast(k + 'reset-' + form[k].validationId);
237+
}
224238
}
225239
}
226240
};

0 commit comments

Comments
 (0)