angularjs - Adding form validation to custom directive in angular -
i aware question has been asked before not know how implement asnwers problem. have tri-state boolean directive in angular , not sure how add validation states directive form can see value valid (not null)
app.directive('customboolean', function(){ return { restrict: 'e', replace: true, scope: { boolvalue: '=', required: '@' }, template: '<button class="btn btn-default btn-xs" ng-click="toggle()" style="width: 70px">{{ boolvalue | bool_to_string }}</button>', controller: function ($scope, $element){ $scope.toggle = function(){ if ($scope.boolvalue == null) { $scope.boolvalue = true; } else if ($scope.boolvalue == true) { $scope.boolvalue = false; } else { $scope.boolvalue = $scope.required ? true : undefined; } }; } } });
below plnkr link of have far. have ideas?
use ngmodel instead of boolvalue. require
ngmodel in directive, , set validity using $setvalidity.
app.directive('customboolean', function(){ return { restrict: 'e', require : 'ngmodel' replace: true, scope: { boolvalue: '=', required: '@' }, template: '<button class="btn btn-default btn-xs" ng-click="toggle()" style="width: 70px">{{ boolvalue | bool_to_string }}</button>', link: function ($scope, $element,$attrs,ngmodel){ if (//your invalid conditions){ ngmodel.$setvalidity(attrs.ngmodel,true/false); } $scope.toggle = function(){ if ($scope.boolvalue == null) { $scope.boolvalue = true; } else if ($scope.boolvalue == true) { $scope.boolvalue = false; } else { $scope.boolvalue = $scope.required ? true : undefined; } }; }}});
Comments
Post a Comment