This repository has been archived by the owner on Aug 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #759 from Gym/client-side-validation
client-side form validation with ng-messages.
- Loading branch information
Showing
20 changed files
with
274 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
modules/core/client/directives/show-errors.client.directives.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Edits by Ryan Hutchison | ||
* Credit: https://github.com/paulyoder/angular-bootstrap-show-errors */ | ||
|
||
angular.module('core') | ||
.directive('showErrors', ['$timeout', '$interpolate', function ($timeout, $interpolate) { | ||
var linkFn = function (scope, el, attrs, formCtrl) { | ||
var inputEl, inputName, inputNgEl, options, showSuccess, toggleClasses, | ||
initCheck = false, | ||
showValidationMessages = false, | ||
blurred = false; | ||
|
||
options = scope.$eval(attrs.showErrors) || {}; | ||
showSuccess = options.showSuccess || false; | ||
inputEl = el[0].querySelector('.form-control[name]') || el[0].querySelector('[name]'); | ||
inputNgEl = angular.element(inputEl); | ||
inputName = $interpolate(inputNgEl.attr('name') || '')(scope); | ||
|
||
if (!inputName) { | ||
throw 'show-errors element has no child input elements with a \'name\' attribute class'; | ||
} | ||
|
||
var reset = function () { | ||
return $timeout(function () { | ||
el.removeClass('has-error'); | ||
el.removeClass('has-success'); | ||
showValidationMessages = false; | ||
}, 0, false); | ||
}; | ||
|
||
scope.$watch(function () { | ||
return formCtrl[inputName] && formCtrl[inputName].$invalid; | ||
}, function (invalid) { | ||
return toggleClasses(invalid); | ||
}); | ||
|
||
scope.$on('show-errors-check-validity', function (event, name) { | ||
if (angular.isUndefined(name) || formCtrl.$name === name) { | ||
initCheck = true; | ||
showValidationMessages = true; | ||
|
||
return toggleClasses(formCtrl[inputName].$invalid); | ||
} | ||
}); | ||
|
||
scope.$on('show-errors-reset', function (event, name) { | ||
if (angular.isUndefined(name) || formCtrl.$name === name) { | ||
return reset(); | ||
} | ||
}); | ||
|
||
toggleClasses = function (invalid) { | ||
el.toggleClass('has-error', showValidationMessages && invalid); | ||
if (showSuccess) { | ||
return el.toggleClass('has-success', showValidationMessages && !invalid); | ||
} | ||
}; | ||
}; | ||
|
||
return { | ||
restrict: 'A', | ||
require: '^form', | ||
compile: function (elem, attrs) { | ||
if (attrs.showErrors.indexOf('skipFormGroupCheck') === -1) { | ||
if (!(elem.hasClass('form-group') || elem.hasClass('input-group'))) { | ||
throw 'show-errors element does not have the \'form-group\' or \'input-group\' class'; | ||
} | ||
} | ||
return linkFn; | ||
} | ||
}; | ||
}]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.