Skip to content

Commit

Permalink
Allow validate/reset only one scope model
Browse files Browse the repository at this point in the history
  • Loading branch information
hueitan committed Jul 10, 2014
1 parent 343f578 commit e16c592
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 23 deletions.
37 changes: 26 additions & 11 deletions dist/angular-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,13 @@
return deferred.promise;
}

for (var k in form) {
if (form[k].hasOwnProperty('$dirty')) {
$scope.$broadcast(k + 'submit-' + form[k].validationId, idx++);
if (form.validationId) {
$scope.$broadcast(form.$name + 'submit-' + form.validationId, idx++);
} else {
for (var k in form) {
if (form[k] && form[k].hasOwnProperty('$dirty')) {
$scope.$broadcast(k + 'submit-' + form[k].validationId, idx++);
}
}
}

Expand Down Expand Up @@ -221,9 +225,19 @@
* @param form
*/
this.reset = function (form) {
for (var k in form) {
if (form[k].hasOwnProperty('$dirty')) {
$scope.$broadcast(k + 'reset-' + form[k].validationId);

if (form === undefined) {
console.error('This is not a regular Form name scope');
return;
}

if (form.validationId) {
$scope.$broadcast(form.$name + 'reset-' + form.validationId);
} else {
for (var k in form) {
if (form[k].hasOwnProperty('$dirty')) {
$scope.$broadcast(k + 'reset-' + form[k].validationId);
}
}
}
};
Expand Down Expand Up @@ -552,13 +566,13 @@
priority: 1, // execute before ng-click (0)
terminal: true,
link: function postLink(scope, element, attrs) {
var form = attrs.validationSubmit;
var form = $parse(attrs.validationSubmit)(scope);

$timeout(function () {
element.on('click', function (e) {
e.preventDefault();

$validationProvider.validate(scope[form])
$validationProvider.validate(form)
.success(function () {
$parse(attrs.ngClick)(scope);
});
Expand All @@ -572,16 +586,17 @@
.directive('validationReset', ['$injector', function ($injector) {

var $validationProvider = $injector.get('$validation'),
$timeout = $injector.get('$timeout');
$timeout = $injector.get('$timeout'),
$parse = $injector.get('$parse');

return {
link: function postLink(scope, element, attrs) {
var form = attrs.validationReset;
var form = $parse(attrs.validationReset)(scope);

$timeout(function () {
element.on('click', function (e) {
e.preventDefault();
$validationProvider.reset(scope[form]);
$validationProvider.reset(form);
});
});

Expand Down
2 changes: 1 addition & 1 deletion dist/angular-validation.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions src/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,13 @@
priority: 1, // execute before ng-click (0)
terminal: true,
link: function postLink(scope, element, attrs) {
var form = attrs.validationSubmit;
var form = $parse(attrs.validationSubmit)(scope);

$timeout(function () {
element.on('click', function (e) {
e.preventDefault();

$validationProvider.validate(scope[form])
$validationProvider.validate(form)
.success(function () {
$parse(attrs.ngClick)(scope);
});
Expand All @@ -315,16 +315,17 @@
.directive('validationReset', ['$injector', function ($injector) {

var $validationProvider = $injector.get('$validation'),
$timeout = $injector.get('$timeout');
$timeout = $injector.get('$timeout'),
$parse = $injector.get('$parse');

return {
link: function postLink(scope, element, attrs) {
var form = attrs.validationReset;
var form = $parse(attrs.validationReset)(scope);

$timeout(function () {
element.on('click', function (e) {
e.preventDefault();
$validationProvider.reset(scope[form]);
$validationProvider.reset(form);
});
});

Expand Down
26 changes: 20 additions & 6 deletions src/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,13 @@
return deferred.promise;
}

for (var k in form) {
if (form[k].hasOwnProperty('$dirty')) {
$scope.$broadcast(k + 'submit-' + form[k].validationId, idx++);
if (form.validationId) {
$scope.$broadcast(form.$name + 'submit-' + form.validationId, idx++);
} else {
for (var k in form) {
if (form[k] && form[k].hasOwnProperty('$dirty')) {
$scope.$broadcast(k + 'submit-' + form[k].validationId, idx++);
}
}
}

Expand Down Expand Up @@ -218,9 +222,19 @@
* @param form
*/
this.reset = function (form) {
for (var k in form) {
if (form[k].hasOwnProperty('$dirty')) {
$scope.$broadcast(k + 'reset-' + form[k].validationId);

if (form === undefined) {
console.error('This is not a regular Form name scope');
return;
}

if (form.validationId) {
$scope.$broadcast(form.$name + 'reset-' + form.validationId);
} else {
for (var k in form) {
if (form[k].hasOwnProperty('$dirty')) {
$scope.$broadcast(k + 'reset-' + form[k].validationId);
}
}
}
};
Expand Down

0 comments on commit e16c592

Please sign in to comment.