javascript - How to validate element directive in angularjs -
i want validate multi-select in angularjs. @ least 1 item should selected in multi-select. if not submit button should not enable. using require validate form. able make compulsory selection of first name using require, how compulsory multiselect.
form
<form ng-submit='addstudent()' name='studentform' novalidate=""> <div class="col-md-4"> <div class="col-md-12"> <label for="name">first name *</label> <input ng-model='student.first_name' name="name" type="text" placeholder="first name" required class="form-control"> </div> </div> <div class="col-md-4"> <div class="col-md-12"> <label for="name">last name </label> <input ng-model='student.last_name' name="name" type="text" placeholder="last name" class="form-control"> </div> </div> <multi-selection selectedsubjs="student.selectedsubjects" allsubjects="allsubjects" left-title="all subjects" right-title="selected subjects"> </multi-selection> <input ng-disabled="sutudentform.$invalid" type="submit"> </form>
element directive
app.directive('multiselection', function(){ return { restrict: 'e', scope: { allsubjects: '=', selectedsubjs: '=', displayattr: '@', lefttitle: '@', righttitle: '@' }, templateurl: "templates/multiselection.html", link: function(scope) { scope.swapsubject1 = function(item) { var index = scope.allsubjects.indexof(item); scope.allsubjects.splice(index, 1); scope.selectedsubjs.push(item); } scope.swapsubject2 = function(item) { var index = scope.selectedsubjs.indexof(item); scope.selectedsubjs.splice(index, 1); scope.allsubjects.push(item); } } }; });
multi-select template
<tr> <td> <div class="entbox"> <switchitem ng-repeat="subj in allsubjects" value="subj.name" ng-click="swapsubject1(item)" ng-model="allsubjects"></switchitem> </div> </td> <td> <div class="entbox"> <switchitem ng-repeat="subj in selectedsubjs" value="subj.name" ng-click="swapsubject2(item)" ng-model="student.selectedsubjects"></switchitem> </div> </td> </tr>
check element not input type set
if (!element.isinput()) { element[0].setcustomvalidity(errormsg); }
Comments
Post a Comment