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

Commit bcce78a

Browse files
committed
fix(ngRequired): set valid to false if select value is required (value is not in options)
1 parent ca7f4a3 commit bcce78a

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

src/ng/directive/ngOptions.js

+36-5
Original file line numberDiff line numberDiff line change
@@ -510,11 +510,6 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
510510

511511
} else {
512512

513-
ngModelCtrl.$isEmpty = function(value) {
514-
return !value || value.length === 0;
515-
};
516-
517-
518513
selectCtrl.writeValue = function writeNgOptionsMultiple(value) {
519514
options.items.forEach(function(option) {
520515
option.element.selected = false;
@@ -558,6 +553,42 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
558553
}
559554
}
560555

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+
583+
// Copy the implementation of $isEmpty function to be used in overwritten one
584+
var $$isEmpty = ngModelCtrl.$isEmpty;
585+
586+
ngModelCtrl.$isEmpty = function(value) {
587+
if ($$isEmpty(value)) {
588+
return true;
589+
}
590+
return !isViewOptionValid(value);
591+
};
561592

562593
if (providedEmptyOption) {
563594

0 commit comments

Comments
 (0)