Last active
November 2, 2015 18:07
-
-
Save kapusta/817b2ae10e208c5b0c93 to your computer and use it in GitHub Desktop.
fooMultiselectRequired lets you set a ui-select as required
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
/** | |
@memberof yourApp | |
@ngdoc directive | |
@name fooMultiselectRequired | |
@description ui-select does not handle required correctly when it's a multi-select, so here's a directive to fix that. See [this github issue](https://github.com/angular-ui/ui-select/issues/258) for more info. This directive is based on LeleDev's example, but adds an extra check. | |
@returns {boolean} | |
@example <ui-select multiple foo-multiselect-required name="whatever" ng-model="ctrl.payload.wahtever" theme="select2"> | |
*/ | |
(function(angular){ | |
'use strict'; | |
angular.module('yourApp').directive('fooMultiselectRequired', function($log){ | |
$log.log('fooMultiselectRequired directive is running.'); | |
return { | |
restrict:'A', | |
require:'ngModel', | |
link:function(scope, elem, attrs, ngModel){ | |
ngModel.$validators.arrayRequired = function(modelValue, viewValue){ | |
return ((modelValue && modelValue.length > 0) ? true : false); | |
}; | |
} | |
}; | |
}); | |
}(window.angular)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment