Skip to content

Instantly share code, notes, and snippets.

@felipeclopes
Last active December 20, 2015 02:38
Show Gist options
  • Save felipeclopes/6057267 to your computer and use it in GitHub Desktop.
Save felipeclopes/6057267 to your computer and use it in GitHub Desktop.
AngularJS: Require Multiple for Select2 Validation
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)
@stherrienaspnet
Copy link

Did you face any issue with IE9?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment