Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 028dc11

Browse files
committed
fix(ngRequired): set valid to false if select value is required (add isSelectedOptionValid function)
1 parent 08aad22 commit 028dc11

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

src/ng/directive/ngOptions.js

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,34 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
387387
selectValueMap[selectValue] = optionItem;
388388
}
389389

390+
/**
391+
*
392+
* @returns {boolean}
393+
*/
394+
function isSelectedOptionValid(){
395+
396+
var selectedValue = selectElement.val();
397+
398+
if(!selectedValue){
399+
return false;
400+
}
401+
402+
// in case select is multiple then check
403+
// if one of the selected values matched with the options return true
404+
// or return false if no value match
405+
if (angular.isArray(selectedValue)) {
406+
for (var i = 0, length = selectedValue.length; i < length; i++) {
407+
if (selectValueMap[selectedValue[i]]) {
408+
return true
409+
}
410+
}
411+
return false
412+
}
413+
414+
return !!selectValueMap[selectedValue];
415+
416+
}
417+
390418
return {
391419
items: optionItems,
392420
selectValueMap: selectValueMap,
@@ -397,7 +425,9 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
397425
// If the viewValue could be an object that may be mutated by the application,
398426
// we need to make a copy and not return the reference to the value on the option.
399427
return trackBy ? angular.copy(option.viewValue) : option.viewValue;
400-
}
428+
},
429+
430+
isSelectedOptionValid: isSelectedOptionValid
401431
};
402432
}
403433
};
@@ -584,10 +614,7 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
584614
var $$isEmpty = ngModelCtrl.$isEmpty;
585615

586616
ngModelCtrl.$isEmpty = function(value) {
587-
if ($$isEmpty(value)) {
588-
return true;
589-
}
590-
return !isViewOptionValid(value);
617+
return $$isEmpty(value) || !options.isSelectedOptionValid();
591618
};
592619

593620
if (providedEmptyOption) {
@@ -762,7 +789,7 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
762789
ngModelCtrl.$render();
763790

764791
// Check to see if the value has changed due to the update to the options
765-
if (!ngModelCtrl.$isEmpty(previousValue)) {
792+
if (!$$isEmpty(previousValue)) {
766793
var nextValue = selectCtrl.readValue();
767794
var isNotPrimitive = ngOptions.trackBy || multiple;
768795
if (isNotPrimitive ? !equals(previousValue, nextValue) : previousValue !== nextValue) {

0 commit comments

Comments
 (0)