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

#258 Validates ng-required when multiple is used. #422

Closed
wants to merge 1 commit into from
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
9 changes: 9 additions & 0 deletions src/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,15 @@
$select.onSelectCallback = $parse(attrs.onSelect);
$select.onRemoveCallback = $parse(attrs.onRemove);

// When configured for multiple, override $isEmpty so that ng-required works as expected.
if ($select.multiple) {
var defaultIsEmptyFn = ngModel.$isEmpty;

ngModel.$isEmpty = function(value) {
return (angular.isArray(value) && value.length === 0) || defaultIsEmptyFn(value);
};
}

//From view --> model
ngModel.$parsers.unshift(function (inputValue) {
var locals = {},
Expand Down
7 changes: 7 additions & 0 deletions test/select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1520,6 +1520,13 @@ describe('ui-select tests', function() {
.toBe("Wladimir <[email protected]>Samantha <[email protected]>Nicole <[email protected]>");

});

it('should be marked invalid when required and empty', function() {
scope.selection.selectedMultiple = [];
var el = createUiSelectMultiple({required: true});
expect(el.scope().$select.ngModel.$invalid).toBe(true);
expect(el.scope().$select.ngModel.$error.required).toBe(true);
});
});

describe('default configuration via uiSelectConfig', function() {
Expand Down