From f8dcdee6c0ba45fc68addc6962b46b41c2176cf2 Mon Sep 17 00:00:00 2001 From: Slawek Kolodziej Date: Thu, 18 Jun 2015 23:15:52 +0200 Subject: [PATCH] Fix validating multiselect fields with 'required' attribute --- src/uiSelectMultipleDirective.js | 4 ++++ test/select.spec.js | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/uiSelectMultipleDirective.js b/src/uiSelectMultipleDirective.js index 261640105..931fedbdc 100644 --- a/src/uiSelectMultipleDirective.js +++ b/src/uiSelectMultipleDirective.js @@ -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 diff --git a/test/select.spec.js b/test/select.spec.js index 3663ab839..e0c3ad741 100644 --- a/test/select.spec.js +++ b/test/select.spec.js @@ -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 () {