-
Notifications
You must be signed in to change notification settings - Fork 1.8k
style(ngRequired): mark when invalid #93
Conversation
style(ngRequired): mark when invalid
Hey guys, should this be working for the multiple attribute as well? |
+1 for multiple |
+1 more for multiple. Hitting a roadblock now that I find out this isn't supported. Has anyone come up with a solution? |
@abobwhite Yes, you can watch the ng-model, if the selected array is empty $setValidity("required", false), otherwise $setValidity("required", true) |
+1 for multiple |
@hatpick thank you! I got it working like a charm with the following directive:
|
Better version without "$watch": angular.module( "myApp" )
.directive( 'uiSelectRequired', function() {
return {
require: 'ngModel',
link: function( scope, element, attrs, ngModelController ) {
var idName = attrs.name;
ngModelController.$validators[idName] = function( modelValue, viewValue ) {
return modelValue !== undefined && modelValue !== null && modelValue && modelValue.length > 0;
};
}
};
}); |
Cool!! |
I use this with angular-fromly for validate ui-select with multiple:
Template:
|
Hey, guys! .directive('uiSelectRequired', function(){
return {
restrict: "A",
require: '^ngModel',
scope: {
uiSelectRequired:"="
},
link: function(scope, elm, attrs, ngModel) {
ngModel.$validators.uiSelectRequired = function(modelValue, viewValue) {
var determineVal;
if (angular.isArray(modelValue)) {
determineVal = modelValue;
} else if (angular.isArray(viewValue)) {
determineVal = viewValue;
}
return scope.uiSelectRequired ? determineVal.length > 0 : true;
};
}
};
}) Template: <ui-select multiple ng-model="...">
<ui-select-match ui-select-required="!filterModel.allOptions">
{{$item.name}}
</ui-select-match>
<ui-select-choice>
<div ng-bind-html="item.name | highlight: $select.search"></div>
</ui-select-choices>
</ui-select> |
You are using It is because I used:
Is my way right or wrong? |
@dsalvagni I misread your template and added ui-select-required to the ui-select element and had a problem with two directives wanting different isolate scopes. it was easily fixed with removing the scope: from the directive object and replacing the return line with.
(could also be It makes more sense to me to put this on the ui-select element since that's the one with the ng-model on it. (similar to ng-model and ng-required elsewhere). |
Great point, @arkarkark. Thanks for sharing it. You're right, it makes more sense. I tried to add The second point, about the return statement, I think this is more readable: return scope.$eval(attrs.uiSelectRequired) ? determineVal.length > 0 : true; |
yeah, I kinda agree wth you on the return value @dsalvagni 👍 |
When using ng-required a red border on control is shown (styles added all 3 themes)
The styles will show only if the control is dirty and invalid, so to reproduce it you should first fill values on each field and then press
Clear ng-model
You can try it with this plunker