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

Commit b1946fd

Browse files
committed
fix(ngRequired): set isOptionvalid flag during writting the value
1 parent 20e2245 commit b1946fd

File tree

1 file changed

+12
-35
lines changed

1 file changed

+12
-35
lines changed

src/ng/directive/ngOptions.js

+12-35
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
414414
var selectCtrl = ctrls[0];
415415
var ngModelCtrl = ctrls[1];
416416
var multiple = attr.multiple;
417+
var isOptionValid = true;
417418

418419
// The emptyOption allows the application developer to provide their own custom "empty"
419420
// option when the viewValue does not match any of the option values.
@@ -466,7 +467,7 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
466467

467468
selectCtrl.writeValue = function writeNgOptionsValue(value) {
468469
var option = options.getOptionFromViewValue(value);
469-
470+
isOptionValid = option ? true : false;
470471
if (option && !option.disabled) {
471472
if (selectElement[0].value !== option.selectValue) {
472473
removeUnknownOption();
@@ -516,10 +517,16 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
516517
});
517518

518519
if (value) {
520+
var matchedOptions = 0;
519521
value.forEach(function(item) {
520522
var option = options.getOptionFromViewValue(item);
521-
if (option && !option.disabled) option.element.selected = true;
523+
if (option && !option.disabled) {
524+
++matchedOptions;
525+
option.element.selected = true;
526+
}
522527
});
528+
529+
isOptionValid = (matchedOptions > 0) ? true : false;
523530
}
524531
};
525532

@@ -553,41 +560,11 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
553560
}
554561
}
555562

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
563+
// Copy the implementation of $isEmpty function to be used in overwritten one.
584564
var $$isEmpty = ngModelCtrl.$isEmpty;
585565

586566
ngModelCtrl.$isEmpty = function(value) {
587-
if ($$isEmpty(value)) {
588-
return true;
589-
}
590-
return !isViewOptionValid(value);
567+
return $$isEmpty(value) || !isOptionValid;
591568
};
592569

593570
if (providedEmptyOption) {
@@ -762,7 +739,7 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
762739
ngModelCtrl.$render();
763740

764741
// Check to see if the value has changed due to the update to the options
765-
if (!ngModelCtrl.$isEmpty(previousValue)) {
742+
if (!$$isEmpty(previousValue)) {
766743
var nextValue = selectCtrl.readValue();
767744
var isNotPrimitive = ngOptions.trackBy || multiple;
768745
if (isNotPrimitive ? !equals(previousValue, nextValue) : previousValue !== nextValue) {

0 commit comments

Comments
 (0)