javascript - Validating the date field's using angularJS -
i using angularjs validation purpose in project , know validate normal fields. now, have date field parameter passing in name attribute. in cases user can click 'add date' button adding more dates. so, in case have written code passing parameter name attribute. because of couldn't name field validate.
even if user add more date fields enter want validate date fields.
here code using,
<div class="col-md-12 col-sm-12 col-xs-12 table-responsive"> <table class="table table-striped table-hover"> <tr ng-repeat="dates in hall.halldates"> <td> <div class="col-md-6 col-sm-6 p-0 p-t-10"> select date<span style="color: red">*</span> </div> <div class="col-md-6 col-sm-6 p-0 p-t-10"> <input type="text" ng-model="dates.halldate" name="halldate{{ $index }}" class="form-control" placeholder="dd-mm-yyyy" mydatepicker readonly="true" ng-class="{ validatefields: submitted && createhallform.date.$invalid }" required> <div ng-show="submitted && createhallform.date.$invalid"> <span class="validatefields" ng-show="createhallform.date.$error.required" >please enter date</span> </div> </div>
how name field parameter apply validation n number of fields? can me know this?
it's you're running angular 1.2 or below. in versions cannot dynamically set name
attribute of element. can confirm looking in rendered dom , still seeing name="halldate{{ $index }}"
after render.
see related github issue here: https://github.com/angular/angular.js/issues/1404
solution 1 upgrade 1.3.x
solution 2 use following workaround posted in github issue referenced above.
// app module angular.module('app', []) // workaround bug #1404 // https://github.com/angular/angular.js/issues/1404 // source: http://plnkr.co/edit/hsmzwc?p=preview .config(['$provide', function($provide) { $provide.decorator('ngmodeldirective', ['$delegate', function($delegate) { var ngmodel = $delegate[0], controller = ngmodel.controller; ngmodel.controller = ['$scope', '$element', '$attrs', '$injector', function(scope, element, attrs, $injector) { var $interpolate = $injector.get('$interpolate'); attrs.$set('name', $interpolate(attrs.name || '')(scope)); $injector.invoke(controller, this, { '$scope': scope, '$element': element, '$attrs': attrs }); }]; return $delegate; }]); $provide.decorator('formdirective', ['$delegate', function($delegate) { var form = $delegate[0], controller = form.controller; form.controller = ['$scope', '$element', '$attrs', '$injector', function(scope, element, attrs, $injector) { var $interpolate = $injector.get('$interpolate'); attrs.$set('name', $interpolate(attrs.name || attrs.ngform || '')(scope)); $injector.invoke(controller, this, { '$scope': scope, '$element': element, '$attrs': attrs }); }]; return $delegate; }]); }]);
Comments
Post a Comment