Last active
December 20, 2015 02:38
-
-
Save felipeclopes/6057267 to your computer and use it in GitHub Desktop.
AngularJS: Require Multiple for Select2 Validation
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
App.directive 'requiredMultiple', -> | |
isEmpty = (value) -> | |
angular.isUndefined(value) || (angular.isArray(value) && value.length == 0) || value == '' || value == null || value != value | |
require: '?ngModel', | |
link: (scope, elm, attr, ctrl) -> | |
return unless ctrl | |
attr.required = true # force truthy in case we are on non input element | |
validator = (value) -> | |
if attr.required && (isEmpty(value) || value == false) | |
ctrl.$setValidity('required', false) | |
else | |
ctrl.$setValidity('required', true) | |
value | |
ctrl.$formatters.push(validator) | |
ctrl.$parsers.unshift(validator) | |
attr.$observe 'required', -> | |
validator(ctrl.$viewValue) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Did you face any issue with IE9?