Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

support ng-required validator - fixes #258 #1764

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/uiSelectDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ uis.directive('uiSelect',
$select.disabled = attrs.disabled !== undefined ? attrs.disabled : false;
});

attrs.$observe('required', function() {
// No need to use $eval() (thanks to ng-required) since we already get a boolean instead of a string
$select.required = attrs.required !== undefined ? attrs.required : false;
});

attrs.$observe('resetSearchInput', function() {
// $eval() is needed otherwise we get a string instead of a boolean
var resetSearchInput = scope.$eval(attrs.resetSearchInput);
Expand Down Expand Up @@ -361,7 +366,7 @@ uis.directive('uiSelect',
};

var opened = false;

scope.calculateDropdownPos = function() {
if ($select.open) {
dropdown = angular.element(element).querySelectorAll('.ui-select-dropdown');
Expand Down
16 changes: 15 additions & 1 deletion src/uiSelectSingleDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,24 @@ uis.directive('uiSelectSingle', ['$timeout','$compile', function($timeout, $comp
if (ngModel.$viewValue !== newValue) {
ngModel.$setViewValue(newValue);
}

if ($select.required && ngModel.$viewValue) {
$select.required = false;
}
});

// If value is set then it cannot be required anymore, has value!
scope.$watch('$select.required', function(newValue) {
if ((typeof ngModel.$viewValue === 'object') && $select.required) {
$select.required = false;
}
});

ngModel.$render = function() {
$select.selected = ngModel.$viewValue;
if ((typeof ngModel.$viewValue === 'object') && $select.required) {
$select.required = false;
}
};

scope.$on('uis:select', function (event, item) {
Expand All @@ -65,7 +79,7 @@ uis.directive('uiSelectSingle', ['$timeout','$compile', function($timeout, $comp
});

//Idea from: https://github.com/ivaynberg/select2/blob/79b5bf6db918d7560bdd959109b7bcfb47edaf43/select2.js#L1954
var focusser = angular.element("<input ng-disabled='$select.disabled' class='ui-select-focusser ui-select-offscreen' type='text' id='{{ $select.focusserId }}' aria-label='{{ $select.focusserTitle }}' aria-haspopup='true' role='button' />");
var focusser = angular.element("<input ng-disabled='$select.disabled' ng-required='$select.required' class='ui-select-focusser ui-select-offscreen' type='text' id='{{ $select.focusserId }}' aria-label='{{ $select.focusserTitle }}' aria-haspopup='true' role='button' />");
$compile(focusser)(scope);
$select.focusser = focusser;

Expand Down