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

Commit e27f25a

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

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/ng/directive/ngOptions.js

+33
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,39 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
728728
});
729729
removeExcessElements(currentElement);
730730

731+
ngModelCtrl.$validators.required = function(modelValue, viewValue) {
732+
733+
if (!attr.required) {
734+
return true;
735+
} else if (ngModelCtrl.$isEmpty(viewValue)) {
736+
return false;
737+
}
738+
739+
var isValidOption = true;
740+
var viewOptions = [];
741+
// Get all option and add them to viewOptions array
742+
angular.forEach(options.items, function(item) {
743+
viewOptions.push(options.getViewValueFromOption(item));
744+
});
745+
746+
// In case of multiple view model is an array so validate all view values
747+
// if one of them doesn't match set isValidOption to false
748+
if (multiple && viewValue) {
749+
for (var i = 0, length = viewValue.length; i < length; i++) {
750+
if (viewOptions.indexOf(viewValue[i]) === -1) {
751+
isValidOption = false;
752+
break;
753+
}
754+
}
755+
} else {
756+
if (viewOptions.indexOf(viewValue) === -1) {
757+
isValidOption = false;
758+
}
759+
}
760+
761+
return isValidOption;
762+
};
763+
731764
ngModelCtrl.$render();
732765

733766
// Check to see if the value has changed due to the update to the options

0 commit comments

Comments
 (0)