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

Commit e48a6b0

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

File tree

1 file changed

+33
-34
lines changed

1 file changed

+33
-34
lines changed

Diff for: src/ng/directive/ngOptions.js

+33-34
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 (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
};
@@ -552,42 +582,11 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
552582

553583
}
554584
}
555-
556-
function isViewOptionValid(viewValue) {
557-
558-
var isValidOption = false;
559-
var viewOptions = [];
560-
// Get all option and add them to viewOptions array
561-
angular.forEach(options.items, function(item) {
562-
viewOptions.push(options.getViewValueFromOption(item));
563-
});
564-
565-
// In case of multiple view is an array so validate all view values
566-
// if one of them match set isValidOption to true
567-
if (multiple) {
568-
for (var i = 0, length = viewValue.length; i < length; i++) {
569-
if (viewOptions.indexOf(viewValue[i]) > -1) {
570-
isValidOption = true;
571-
break;
572-
}
573-
}
574-
} else {
575-
if (viewOptions.indexOf(viewValue) > -1) {
576-
isValidOption = true;
577-
}
578-
}
579-
580-
return isValidOption;
581-
}
582-
583585
// Copy the implementation of $isEmpty function to be used in overwritten one
584586
var $$isEmpty = ngModelCtrl.$isEmpty;
585587

586588
ngModelCtrl.$isEmpty = function(value) {
587-
if ($$isEmpty(value)) {
588-
return true;
589-
}
590-
return !isViewOptionValid(value);
589+
return $$isEmpty(value) || !options.isSelectedOptionValid();
591590
};
592591

593592
if (providedEmptyOption) {
@@ -762,7 +761,7 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
762761
ngModelCtrl.$render();
763762

764763
// Check to see if the value has changed due to the update to the options
765-
if (!ngModelCtrl.$isEmpty(previousValue)) {
764+
if (!$$isEmpty(previousValue)) {
766765
var nextValue = selectCtrl.readValue();
767766
var isNotPrimitive = ngOptions.trackBy || multiple;
768767
if (isNotPrimitive ? !equals(previousValue, nextValue) : previousValue !== nextValue) {

0 commit comments

Comments
 (0)