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

Fix validating multiselect fields with 'required' attribute #1026

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
4 changes: 4 additions & 0 deletions src/uiSelectMultipleDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ uis.directive('uiSelectMultiple', ['uiSelectMinErr','$timeout', function(uiSelec
return resultMultiple;
});

ngModel.$isEmpty = function () {
return !(angular.isArray(ngModel.$$rawModelValue) && ngModel.$$rawModelValue.length > 0);
};

// From model --> view
ngModel.$formatters.unshift(function (inputValue) {
var data = $select.parserResult.source (scope, { $select : {search:''}}), //Overwrite $search
Expand Down
19 changes: 19 additions & 0 deletions test/select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2102,6 +2102,25 @@ describe('ui-select tests', function() {
expect(searchEl.length).toEqual(1);
expect(searchEl[0].id).toEqual('inid');
});

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

expect(el.scope().$select.ngModel.$invalid).toEqual(true);
expect(el.scope().$select.ngModel.$error.required).toEqual(true);

clickItem(el, 'Samantha');

expect(el.scope().$select.ngModel.$invalid).toEqual(false);
expect(el.scope().$select.ngModel.$error.required).toEqual(undefined);

el.find('.ui-select-match-item').first().find('.ui-select-match-close').click();
$timeout.flush();

expect(el.scope().$select.ngModel.$invalid).toEqual(true);
expect(el.scope().$select.ngModel.$error.required).toEqual(true);
});
});

it('should add an id to the search input field', function () {
Expand Down