Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

Bug/required multiple select #1025

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/select2-bootstrap3.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
<div class="col-sm-6">

<ui-select multiple sortable="true" ng-model="person.selected" theme="select2" class="form-control" title="Choose a person">
<ui-select-match placeholder="Select or search a person in the list...">{{$select.selected.name}}</ui-select-match>
<ui-select-match placeholder="Select or search a person in the list...">{{$item.name}}</ui-select-match>
<ui-select-choices repeat="item in people | filter: $select.search">
<div ng-bind-html="item.name | highlight: $select.search"></div>
<small ng-bind-html="item.email | highlight: $select.search"></small>
Expand Down
30 changes: 30 additions & 0 deletions src/uiSelectDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,36 @@ uis.directive('uiSelect',
element.removeAttr("tabindex");
});
}

if (angular.isDefined(attrs.multiple) && angular.isDefined(attrs.required)) {

// Custom validator to make required work with mulitple
// this pattern modeled after the way angular directly
// works with the requiredDirective
ngModel.$validators.uiRequired = function (modelValue, viewValue) {

// then we're dealing with multiple
if (Array.isArray(modelValue)) {
return modelValue.length > 0;
} else if (Array.isArray(viewValue)) {
return viewValue.length > 0;

// otherwise just check the direct model value
// for single selects
} else {
return modelValue !== undefined;
}

// we have to return a check directly.
// returning a variable can sometimes
// cause the check to return undefined
// to the $invalid and $valid properties
// due to the way angular parses the view.
};
attrs.$observe('required', function() {
ngModel.$validate();
});
}

scope.$watch('searchEnabled', function() {
var searchEnabled = scope.$eval(attrs.searchEnabled);
Expand Down